]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/ShellProtocol.c
fix comments.
[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
c154b997 910 if (Handle1 != NULL) {\r
a405b86d 911 //\r
c154b997 912 // chop off the begining part before the file system part...\r
a405b86d 913 //\r
c154b997 914 ///@todo BlockIo?\r
915 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid,\r
916 &DevicePath,\r
917 &Handle);\r
918 if (!EFI_ERROR(Status)) {\r
a405b86d 919 //\r
c154b997 920 // To access as a file system, the file path should only\r
921 // contain file path components. Follow the file path nodes\r
922 // and find the target file\r
a405b86d 923 //\r
c154b997 924 for ( FilePathNode = (FILEPATH_DEVICE_PATH *)DevicePath\r
925 ; !IsDevicePathEnd (&FilePathNode->Header)\r
926 ; FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header)\r
927 ){\r
928 SHELL_FREE_NON_NULL(AlignedNode);\r
929 AlignedNode = AllocateCopyPool (DevicePathNodeLength(FilePathNode), FilePathNode);\r
a405b86d 930 //\r
c154b997 931 // For file system access each node should be a file path component\r
a405b86d 932 //\r
c154b997 933 if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||\r
934 DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP\r
935 ) {\r
936 Status = EFI_UNSUPPORTED;\r
937 break;\r
938 }\r
a405b86d 939\r
940 //\r
c154b997 941 // Open this file path node\r
a405b86d 942 //\r
c154b997 943 Handle2 = Handle1;\r
944 Handle1 = NULL;\r
945\r
a405b86d 946 //\r
c154b997 947 // if this is the last node in the DevicePath always create (if that was requested).\r
a405b86d 948 //\r
c154b997 949 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode->Header)))) {\r
a405b86d 950 Status = Handle2->Open (\r
951 Handle2,\r
952 &Handle1,\r
8be0ba36 953 AlignedNode->PathName,\r
a405b86d 954 OpenMode,\r
955 Attributes\r
956 );\r
c154b997 957 } else {\r
958\r
959 //\r
960 // This is not the last node and we dont want to 'create' existing\r
961 // directory entries...\r
962 //\r
963\r
964 //\r
965 // open without letting it create\r
966 // prevents error on existing files/directories\r
967 //\r
968 Status = Handle2->Open (\r
969 Handle2,\r
970 &Handle1,\r
971 AlignedNode->PathName,\r
972 OpenMode &~EFI_FILE_MODE_CREATE,\r
973 Attributes\r
974 );\r
975 //\r
976 // if above failed now open and create the 'item'\r
977 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)\r
978 //\r
979 if ((EFI_ERROR (Status)) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)) {\r
980 Status = Handle2->Open (\r
981 Handle2,\r
982 &Handle1,\r
983 AlignedNode->PathName,\r
984 OpenMode,\r
985 Attributes\r
986 );\r
987 }\r
a405b86d 988 }\r
c154b997 989 //\r
990 // Close the last node\r
991 //\r
992 ShellInfoObject.NewEfiShellProtocol->CloseFile (Handle2);\r
a405b86d 993\r
c154b997 994 //\r
995 // If there's been an error, stop\r
996 //\r
997 if (EFI_ERROR (Status)) {\r
998 break;\r
999 }\r
1000 } // for loop\r
1001 }\r
a405b86d 1002 }\r
1003 }\r
8be0ba36 1004 SHELL_FREE_NON_NULL(AlignedNode);\r
a405b86d 1005 if (EFI_ERROR(Status)) {\r
1006 if (Handle1 != NULL) {\r
8be0ba36 1007 ShellInfoObject.NewEfiShellProtocol->CloseFile(Handle1);\r
a405b86d 1008 }\r
1009 } else {\r
1010 *FileHandle = ConvertEfiFileProtocolToShellHandle(Handle1, ShellFileHandleGetPath(ShellHandle));\r
1011 }\r
1012 return (Status);\r
1013}\r
1014\r
1015/**\r
1016 Creates a file or directory by name.\r
1017\r
1018 This function creates an empty new file or directory with the specified attributes and\r
1019 returns the new file's handle. If the file already exists and is read-only, then\r
1020 EFI_INVALID_PARAMETER will be returned.\r
1021\r
1022 If the file already existed, it is truncated and its attributes updated. If the file is\r
1023 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.\r
1024\r
1025 If the file name begins with >v, then the file handle which is returned refers to the\r
1026 shell environment variable with the specified name. If the shell environment variable\r
1027 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.\r
1028\r
1029 @param FileName Pointer to NULL-terminated file path\r
1030 @param FileAttribs The new file's attrbiutes. the different attributes are\r
1031 described in EFI_FILE_PROTOCOL.Open().\r
1032 @param FileHandle On return, points to the created file handle or directory's handle\r
1033\r
1034 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.\r
1035 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1036 @retval EFI_UNSUPPORTED could not open the file path\r
1037 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not\r
1038 file the file system on the device.\r
1039 @retval EFI_NO_MEDIA the device has no medium.\r
1040 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no\r
1041 longer supported.\r
1042 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according\r
1043 the DirName.\r
1044 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1045 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write\r
1046 when the media is write-protected.\r
1047 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
1048 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.\r
1049 @retval EFI_VOLUME_FULL The volume is full.\r
1050**/\r
1051EFI_STATUS\r
1052EFIAPI\r
1053EfiShellCreateFile(\r
1054 IN CONST CHAR16 *FileName,\r
1055 IN UINT64 FileAttribs,\r
1056 OUT SHELL_FILE_HANDLE *FileHandle\r
1057 )\r
1058{\r
1059 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1060 EFI_STATUS Status;\r
1061\r
1062 //\r
1063 // Is this for an environment variable\r
1064 // do we start with >v\r
1065 //\r
1066 if (StrStr(FileName, L">v") == FileName) {\r
1067 if (!IsVolatileEnv(FileName+2)) {\r
1068 return (EFI_INVALID_PARAMETER);\r
1069 }\r
1070 *FileHandle = CreateFileInterfaceEnv(FileName+2);\r
1071 return (EFI_SUCCESS);\r
1072 }\r
1073\r
1074 //\r
1075 // We are opening a regular file.\r
1076 //\r
1077 DevicePath = EfiShellGetDevicePathFromFilePath(FileName);\r
1078 if (DevicePath == NULL) {\r
1079 return (EFI_NOT_FOUND);\r
1080 }\r
1081\r
1082 Status = InternalOpenFileDevicePath(DevicePath, FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, FileAttribs); // 0 = no specific file attributes\r
1083 FreePool(DevicePath);\r
1084\r
1085 return(Status);\r
1086}\r
1087\r
1088/**\r
1089 Opens a file or a directory by file name.\r
1090\r
1091 This function opens the specified file in the specified OpenMode and returns a file\r
1092 handle.\r
1093 If the file name begins with >v, then the file handle which is returned refers to the\r
1094 shell environment variable with the specified name. If the shell environment variable\r
1095 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then\r
1096 EFI_INVALID_PARAMETER is returned.\r
1097\r
1098 If the file name is >i, then the file handle which is returned refers to the standard\r
1099 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER\r
1100 is returned.\r
1101\r
1102 If the file name is >o, then the file handle which is returned refers to the standard\r
1103 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER\r
1104 is returned.\r
1105\r
1106 If the file name is >e, then the file handle which is returned refers to the standard\r
1107 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER\r
1108 is returned.\r
1109\r
1110 If the file name is NUL, then the file handle that is returned refers to the standard NUL\r
1111 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is\r
1112 returned.\r
1113\r
1114 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the\r
1115 FileHandle is NULL.\r
1116\r
1117 @param FileName Points to the NULL-terminated UCS-2 encoded file name.\r
1118 @param FileHandle On return, points to the file handle.\r
1119 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or\r
1120 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI\r
1121 Specification.\r
1122 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.\r
1123 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.\r
1124 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.\r
1125 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file\r
1126 system could not be found on the device. FileHandle is NULL.\r
1127 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.\r
1128 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no\r
1129 longer supported. FileHandle is NULL.\r
1130 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according\r
1131 the FileName. FileHandle is NULL.\r
1132 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.\r
1133 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write\r
1134 when the media is write-protected. FileHandle is NULL.\r
1135 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.\r
1136 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle\r
1137 is NULL.\r
1138 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.\r
1139**/\r
1140EFI_STATUS\r
1141EFIAPI\r
1142EfiShellOpenFileByName(\r
1143 IN CONST CHAR16 *FileName,\r
1144 OUT SHELL_FILE_HANDLE *FileHandle,\r
1145 IN UINT64 OpenMode\r
1146 )\r
1147{\r
1148 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1149 EFI_STATUS Status;\r
1150\r
1151 *FileHandle = NULL;\r
1152\r
1153 //\r
1154 // Is this for StdIn\r
1155 //\r
1156 if (StrCmp(FileName, L">i") == 0) {\r
1157 //\r
1158 // make sure not writing to StdIn\r
1159 //\r
1160 if ((OpenMode & EFI_FILE_MODE_WRITE) != 0) {\r
1161 return (EFI_INVALID_PARAMETER);\r
1162 }\r
1163 *FileHandle = ShellInfoObject.NewShellParametersProtocol->StdIn;\r
1164 ASSERT(*FileHandle != NULL);\r
1165 return (EFI_SUCCESS);\r
1166 }\r
1167\r
1168 //\r
1169 // Is this for StdOut\r
1170 //\r
1171 if (StrCmp(FileName, L">o") == 0) {\r
1172 //\r
1173 // make sure not writing to StdIn\r
1174 //\r
1175 if ((OpenMode & EFI_FILE_MODE_READ) != 0) {\r
1176 return (EFI_INVALID_PARAMETER);\r
1177 }\r
1178 *FileHandle = &FileInterfaceStdOut;\r
1179 return (EFI_SUCCESS);\r
1180 }\r
1181\r
1182 //\r
1183 // Is this for NUL file\r
1184 //\r
1185 if (StrCmp(FileName, L"NUL") == 0) {\r
1186 *FileHandle = &FileInterfaceNulFile;\r
1187 return (EFI_SUCCESS);\r
1188 }\r
1189\r
1190 //\r
1191 // Is this for StdErr\r
1192 //\r
1193 if (StrCmp(FileName, L">e") == 0) {\r
1194 //\r
1195 // make sure not writing to StdIn\r
1196 //\r
1197 if ((OpenMode & EFI_FILE_MODE_READ) != 0) {\r
1198 return (EFI_INVALID_PARAMETER);\r
1199 }\r
1200 *FileHandle = &FileInterfaceStdErr;\r
1201 return (EFI_SUCCESS);\r
1202 }\r
1203\r
1204 //\r
1205 // Is this for an environment variable\r
1206 // do we start with >v\r
1207 //\r
1208 if (StrStr(FileName, L">v") == FileName) {\r
1209 if (!IsVolatileEnv(FileName+2) &&\r
1210 ((OpenMode & EFI_FILE_MODE_WRITE) != 0)) {\r
1211 return (EFI_INVALID_PARAMETER);\r
1212 }\r
1213 *FileHandle = CreateFileInterfaceEnv(FileName+2);\r
1214 return (EFI_SUCCESS);\r
1215 }\r
1216\r
1217 //\r
1218 // We are opening a regular file.\r
1219 //\r
1220 DevicePath = EfiShellGetDevicePathFromFilePath(FileName);\r
1221// DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););\r
1222 if (DevicePath == NULL) {\r
1223 return (EFI_NOT_FOUND);\r
1224 }\r
1225\r
1226 //\r
1227 // Copy the device path, open the file, then free the memory\r
1228 //\r
1229 Status = InternalOpenFileDevicePath(DevicePath, FileHandle, OpenMode, 0); // 0 = no specific file attributes\r
1230 FreePool(DevicePath);\r
1231\r
1232 return(Status);\r
1233}\r
1234\r
1235/**\r
1236 Deletes the file specified by the file name.\r
1237\r
1238 This function deletes a file.\r
1239\r
1240 @param FileName Points to the NULL-terminated file name.\r
1241\r
1242 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.\r
1243 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.\r
1244 @sa EfiShellCreateFile\r
1245**/\r
1246EFI_STATUS\r
1247EFIAPI\r
1248EfiShellDeleteFileByName(\r
1249 IN CONST CHAR16 *FileName\r
1250 )\r
1251{\r
1252 SHELL_FILE_HANDLE FileHandle;\r
1253 EFI_STATUS Status;\r
1254\r
1255 //\r
1256 // get a handle to the file\r
1257 //\r
1258 Status = EfiShellCreateFile(FileName,\r
1259 0,\r
1260 &FileHandle);\r
1261 if (EFI_ERROR(Status)) {\r
1262 return (Status);\r
1263 }\r
1264 //\r
1265 // now delete the file\r
1266 //\r
1267 return (ShellInfoObject.NewEfiShellProtocol->DeleteFile(FileHandle));\r
1268}\r
1269\r
1270/**\r
1271 Disables the page break output mode.\r
1272**/\r
1273VOID\r
1274EFIAPI\r
1275EfiShellDisablePageBreak (\r
1276 VOID\r
1277 )\r
1278{\r
1279 ShellInfoObject.PageBreakEnabled = FALSE;\r
1280}\r
1281\r
1282/**\r
1283 Enables the page break output mode.\r
1284**/\r
1285VOID\r
1286EFIAPI\r
1287EfiShellEnablePageBreak (\r
1288 VOID\r
1289 )\r
1290{\r
1291 ShellInfoObject.PageBreakEnabled = TRUE;\r
1292}\r
1293\r
1294/**\r
1295 internal worker function to load and run an image via device path.\r
1296\r
1297 @param ParentImageHandle A handle of the image that is executing the specified\r
1298 command line.\r
1299 @param DevicePath device path of the file to execute\r
1300 @param CommandLine Points to the NULL-terminated UCS-2 encoded string\r
1301 containing the command line. If NULL then the command-\r
1302 line will be empty.\r
1303 @param Environment Points to a NULL-terminated array of environment\r
1304 variables with the format 'x=y', where x is the\r
1305 environment variable name and y is the value. If this\r
1306 is NULL, then the current shell environment is used.\r
1307 @param StatusCode Points to the status code returned by the command.\r
1308\r
1309 @retval EFI_SUCCESS The command executed successfully. The status code\r
1310 returned by the command is pointed to by StatusCode.\r
1311 @retval EFI_INVALID_PARAMETER The parameters are invalid.\r
1312 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1313 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.\r
1314**/\r
1315EFI_STATUS\r
1316EFIAPI\r
1317InternalShellExecuteDevicePath(\r
1318 IN CONST EFI_HANDLE *ParentImageHandle,\r
1319 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1320 IN CONST CHAR16 *CommandLine OPTIONAL,\r
1321 IN CONST CHAR16 **Environment OPTIONAL,\r
1322 OUT EFI_STATUS *StatusCode OPTIONAL\r
1323 )\r
1324{\r
1325 EFI_STATUS Status;\r
1326 EFI_HANDLE NewHandle;\r
1327 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
1328 LIST_ENTRY OrigEnvs;\r
1329 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol;\r
1330\r
1331 if (ParentImageHandle == NULL) {\r
1332 return (EFI_INVALID_PARAMETER);\r
1333 }\r
1334\r
1335 InitializeListHead(&OrigEnvs);\r
1336\r
1337 NewHandle = NULL;\r
1338\r
1339 //\r
1340 // Load the image with:\r
1341 // FALSE - not from boot manager and NULL, 0 being not already in memory\r
1342 //\r
1343 Status = gBS->LoadImage(\r
1344 FALSE,\r
1345 *ParentImageHandle,\r
1346 (EFI_DEVICE_PATH_PROTOCOL*)DevicePath,\r
1347 NULL,\r
1348 0,\r
1349 &NewHandle);\r
1350\r
1351 if (EFI_ERROR(Status)) {\r
1352 if (NewHandle != NULL) {\r
1353 gBS->UnloadImage(NewHandle);\r
1354 }\r
1355 return (Status);\r
1356 }\r
1357 Status = gBS->OpenProtocol(\r
1358 NewHandle,\r
1359 &gEfiLoadedImageProtocolGuid,\r
1360 (VOID**)&LoadedImage,\r
1361 gImageHandle,\r
1362 NULL,\r
1363 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
1364\r
1365 if (!EFI_ERROR(Status)) {\r
1366 ASSERT(LoadedImage->LoadOptionsSize == 0);\r
1367 if (CommandLine != NULL) {\r
1368 LoadedImage->LoadOptionsSize = (UINT32)StrSize(CommandLine);\r
1369 LoadedImage->LoadOptions = (VOID*)CommandLine;\r
1370 }\r
1371\r
1372 //\r
1373 // Save our current environment settings for later restoration if necessary\r
1374 //\r
1375 if (Environment != NULL) {\r
1376 Status = GetEnvironmentVariableList(&OrigEnvs);\r
1377 if (!EFI_ERROR(Status)) {\r
1378 Status = SetEnvironmentVariables(Environment);\r
1379 }\r
1380 }\r
1381\r
1382 //\r
1383 // Initialize and install a shell parameters protocol on the image.\r
1384 //\r
1385 ShellParamsProtocol.StdIn = ShellInfoObject.NewShellParametersProtocol->StdIn;\r
1386 ShellParamsProtocol.StdOut = ShellInfoObject.NewShellParametersProtocol->StdOut;\r
1387 ShellParamsProtocol.StdErr = ShellInfoObject.NewShellParametersProtocol->StdErr;\r
1388 Status = UpdateArgcArgv(&ShellParamsProtocol, CommandLine, NULL, NULL);\r
1389 ASSERT_EFI_ERROR(Status);\r
1390 Status = gBS->InstallProtocolInterface(&NewHandle, &gEfiShellParametersProtocolGuid, EFI_NATIVE_INTERFACE, &ShellParamsProtocol);\r
1391 ASSERT_EFI_ERROR(Status);\r
1392\r
1393 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)\r
1394\r
1395 //\r
1396 // now start the image and if the caller wanted the return code pass it to them...\r
1397 //\r
1398 if (!EFI_ERROR(Status)) {\r
1399 if (StatusCode != NULL) {\r
1400 *StatusCode = gBS->StartImage(NewHandle, NULL, NULL);\r
1401 } else {\r
1402 Status = gBS->StartImage(NewHandle, NULL, NULL);\r
1403 }\r
1404 }\r
1405\r
1406 //\r
1407 // Cleanup (and dont overwrite errors)\r
1408 //\r
1409 if (EFI_ERROR(Status)) {\r
1410 gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);\r
1411 } else {\r
1412 Status = gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);\r
1413 ASSERT_EFI_ERROR(Status);\r
1414 }\r
1415 }\r
1416\r
1417 if (!IsListEmpty(&OrigEnvs)) {\r
1418 if (EFI_ERROR(Status)) {\r
1419 SetEnvironmentVariableList(&OrigEnvs);\r
1420 } else {\r
1421 Status = SetEnvironmentVariableList(&OrigEnvs);\r
1422 }\r
1423 }\r
1424\r
1425 return(Status);\r
1426}\r
1427/**\r
1428 Execute the command line.\r
1429\r
1430 This function creates a nested instance of the shell and executes the specified\r
1431 command (CommandLine) with the specified environment (Environment). Upon return,\r
1432 the status code returned by the specified command is placed in StatusCode.\r
1433\r
1434 If Environment is NULL, then the current environment is used and all changes made\r
1435 by the commands executed will be reflected in the current environment. If the\r
1436 Environment is non-NULL, then the changes made will be discarded.\r
1437\r
1438 The CommandLine is executed from the current working directory on the current\r
1439 device.\r
1440\r
1441 @param ParentImageHandle A handle of the image that is executing the specified\r
1442 command line.\r
1443 @param CommandLine Points to the NULL-terminated UCS-2 encoded string\r
1444 containing the command line. If NULL then the command-\r
1445 line will be empty.\r
1446 @param Environment Points to a NULL-terminated array of environment\r
1447 variables with the format 'x=y', where x is the\r
1448 environment variable name and y is the value. If this\r
1449 is NULL, then the current shell environment is used.\r
1450 @param StatusCode Points to the status code returned by the command.\r
1451\r
1452 @retval EFI_SUCCESS The command executed successfully. The status code\r
1453 returned by the command is pointed to by StatusCode.\r
1454 @retval EFI_INVALID_PARAMETER The parameters are invalid.\r
1455 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1456 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.\r
1457 @retval EFI_UNSUPPORTED The support level required for this function is not present.\r
1458\r
1459 @sa InternalShellExecuteDevicePath\r
1460**/\r
1461EFI_STATUS\r
1462EFIAPI\r
1463EfiShellExecute(\r
1464 IN EFI_HANDLE *ParentImageHandle,\r
1465 IN CHAR16 *CommandLine OPTIONAL,\r
1466 IN CHAR16 **Environment OPTIONAL,\r
1467 OUT EFI_STATUS *StatusCode OPTIONAL\r
1468 )\r
1469{\r
1470 EFI_STATUS Status;\r
1471 CHAR16 *Temp;\r
1472 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
1473 UINTN Size;\r
1474\r
1475 if ((PcdGet8(PcdShellSupportLevel) < 1)) {\r
1476 return (EFI_UNSUPPORTED);\r
1477 }\r
1478\r
1479 DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
1480\r
1481 DEBUG_CODE_BEGIN();\r
1482 Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE);\r
1483 FreePool(Temp);\r
1484 Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE);\r
1485 FreePool(Temp);\r
1486 Temp = gDevPathToText->ConvertDevicePathToText(DevPath, TRUE, TRUE);\r
1487 FreePool(Temp);\r
1488 DEBUG_CODE_END();\r
1489\r
1490 Temp = NULL;\r
1491 Size = 0;\r
1492 ASSERT((Temp == NULL && Size == 0) || (Temp != NULL));\r
1493 StrnCatGrow(&Temp, &Size, L"Shell.efi ", 0);\r
1494 StrnCatGrow(&Temp, &Size, CommandLine, 0);\r
1495\r
1496 Status = InternalShellExecuteDevicePath(\r
1497 ParentImageHandle,\r
1498 DevPath,\r
1499 Temp,\r
1500 (CONST CHAR16**)Environment,\r
1501 StatusCode);\r
1502\r
1503 //\r
1504 // de-allocate and return\r
1505 //\r
1506 FreePool(DevPath);\r
1507 FreePool(Temp);\r
1508 return(Status);\r
1509}\r
1510\r
1511/**\r
1512 Utility cleanup function for EFI_SHELL_FILE_INFO objects.\r
1513\r
1514 1) frees all pointers (non-NULL)\r
1515 2) Closes the SHELL_FILE_HANDLE\r
1516\r
1517 @param FileListNode pointer to the list node to free\r
1518**/\r
1519VOID\r
1520EFIAPI\r
1521InternalFreeShellFileInfoNode(\r
1522 IN EFI_SHELL_FILE_INFO *FileListNode\r
1523 )\r
1524{\r
1525 if (FileListNode->Info != NULL) {\r
1526 FreePool((VOID*)FileListNode->Info);\r
1527 }\r
1528 if (FileListNode->FileName != NULL) {\r
1529 FreePool((VOID*)FileListNode->FileName);\r
1530 }\r
1531 if (FileListNode->FullName != NULL) {\r
1532 FreePool((VOID*)FileListNode->FullName);\r
1533 }\r
1534 if (FileListNode->Handle != NULL) {\r
1535 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileListNode->Handle);\r
1536 }\r
1537 FreePool(FileListNode);\r
1538}\r
1539/**\r
1540 Frees the file list.\r
1541\r
1542 This function cleans up the file list and any related data structures. It has no\r
1543 impact on the files themselves.\r
1544\r
1545 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is\r
1546 defined in OpenFileList()\r
1547\r
1548 @retval EFI_SUCCESS Free the file list successfully.\r
1549 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;\r
1550**/\r
1551EFI_STATUS\r
1552EFIAPI\r
1553EfiShellFreeFileList(\r
1554 IN EFI_SHELL_FILE_INFO **FileList\r
1555 )\r
1556{\r
1557 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1558\r
1559 if (FileList == NULL || *FileList == NULL) {\r
1560 return (EFI_INVALID_PARAMETER);\r
1561 }\r
1562\r
1563 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
1564 ; !IsListEmpty(&(*FileList)->Link)\r
1565 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
1566 ){\r
1567 RemoveEntryList(&ShellFileListItem->Link);\r
1568 InternalFreeShellFileInfoNode(ShellFileListItem);\r
1569 }\r
1570 return(EFI_SUCCESS);\r
1571}\r
1572\r
1573/**\r
1574 Deletes the duplicate file names files in the given file list.\r
1575\r
1576 This function deletes the reduplicate files in the given file list.\r
1577\r
1578 @param FileList A pointer to the first entry in the file list.\r
1579\r
1580 @retval EFI_SUCCESS Always success.\r
1581 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;\r
1582**/\r
1583EFI_STATUS\r
1584EFIAPI\r
1585EfiShellRemoveDupInFileList(\r
1586 IN EFI_SHELL_FILE_INFO **FileList\r
1587 )\r
1588{\r
1589 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1590 EFI_SHELL_FILE_INFO *ShellFileListItem2;\r
1591\r
1592 if (FileList == NULL || *FileList == NULL) {\r
1593 return (EFI_INVALID_PARAMETER);\r
1594 }\r
1595 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
1596 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)\r
1597 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
1598 ){\r
1599 for ( ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
1600 ; !IsNull(&(*FileList)->Link, &ShellFileListItem2->Link)\r
1601 ; ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem2->Link)\r
1602 ){\r
1603 if (gUnicodeCollation->StriColl(\r
1604 gUnicodeCollation,\r
1605 (CHAR16*)ShellFileListItem->FullName,\r
1606 (CHAR16*)ShellFileListItem2->FullName) == 0\r
1607 ){\r
1608 RemoveEntryList(&ShellFileListItem2->Link);\r
1609 InternalFreeShellFileInfoNode(ShellFileListItem2);\r
1610 }\r
1611 }\r
1612 }\r
1613 return (EFI_SUCCESS);\r
1614}\r
1615/**\r
1616 Allocates and duplicates a EFI_SHELL_FILE_INFO node.\r
1617\r
1618 @param[in] Node The node to copy from.\r
1619 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.\r
1620\r
1621 @retval NULL a memory allocation error ocurred\r
1622 @return != NULL a pointer to the new node\r
1623**/\r
1624EFI_SHELL_FILE_INFO*\r
1625EFIAPI\r
1626InternalDuplicateShellFileInfo(\r
1627 IN EFI_SHELL_FILE_INFO *Node,\r
1628 IN BOOLEAN Save\r
1629 )\r
1630{\r
1631 EFI_SHELL_FILE_INFO *NewNode;\r
1632\r
1633 NewNode = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));\r
1634 if (NewNode == NULL) {\r
1635 return (NULL);\r
1636 }\r
1637 NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName));\r
1638\r
1639 NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName));\r
1640 NewNode->Info = AllocatePool((UINTN)Node->Info->Size);\r
1641 if ( NewNode->FullName == NULL\r
1642 || NewNode->FileName == NULL\r
1643 || NewNode->Info == NULL\r
1644 ){\r
1645 return(NULL);\r
1646 }\r
1647 NewNode->Status = Node->Status;\r
1648 NewNode->Handle = Node->Handle;\r
1649 if (!Save) {\r
1650 Node->Handle = NULL;\r
1651 }\r
1652 StrCpy((CHAR16*)NewNode->FullName, Node->FullName);\r
1653 StrCpy((CHAR16*)NewNode->FileName, Node->FileName);\r
1654 CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size);\r
1655\r
1656 return(NewNode);\r
1657}\r
1658\r
1659/**\r
1660 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation\r
1661 failed it will return NULL.\r
1662\r
1663 @param[in] BasePath the Path to prepend onto filename for FullPath\r
1664 @param[in] Status Status member initial value.\r
1665 @param[in] FullName FullName member initial value.\r
1666 @param[in] FileName FileName member initial value.\r
1667 @param[in] Handle Handle member initial value.\r
1668 @param[in] Info Info struct to copy.\r
1669\r
1670 @retval NULL An error ocurred.\r
1671 @return a pointer to the newly allocated structure.\r
1672**/\r
1673EFI_SHELL_FILE_INFO *\r
1674EFIAPI\r
1675CreateAndPopulateShellFileInfo(\r
1676 IN CONST CHAR16 *BasePath,\r
1677 IN CONST EFI_STATUS Status,\r
1678 IN CONST CHAR16 *FullName,\r
1679 IN CONST CHAR16 *FileName,\r
1680 IN CONST SHELL_FILE_HANDLE Handle,\r
1681 IN CONST EFI_FILE_INFO *Info\r
1682 )\r
1683{\r
1684 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1685 CHAR16 *TempString;\r
1686 UINTN Size;\r
1687\r
1688 TempString = NULL;\r
1689 Size = 0;\r
1690\r
1691 ShellFileListItem = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1692 if (ShellFileListItem == NULL) {\r
1693 return (NULL);\r
1694 }\r
1695 if (Info != NULL) {\r
1696 ShellFileListItem->Info = AllocateZeroPool((UINTN)Info->Size);\r
1697 if (ShellFileListItem->Info == NULL) {\r
1698 FreePool(ShellFileListItem);\r
1699 return (NULL);\r
1700 }\r
1701 CopyMem(ShellFileListItem->Info, Info, (UINTN)Info->Size);\r
1702 } else {\r
1703 ShellFileListItem->Info = NULL;\r
1704 }\r
1705 if (FileName != NULL) {\r
1706 ASSERT(TempString == NULL);\r
1707 ShellFileListItem->FileName = StrnCatGrow(&TempString, 0, FileName, 0);\r
1708 if (ShellFileListItem->FileName == NULL) {\r
1709 FreePool(ShellFileListItem->Info);\r
1710 FreePool(ShellFileListItem);\r
1711 return (NULL);\r
1712 }\r
1713 } else {\r
1714 ShellFileListItem->FileName = NULL;\r
1715 }\r
1716 Size = 0;\r
1717 TempString = NULL;\r
1718 if (BasePath != NULL) {\r
1719 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
1720 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
1721 if (TempString == NULL) {\r
1722 FreePool((VOID*)ShellFileListItem->FileName);\r
1723 FreePool(ShellFileListItem->Info);\r
1724 FreePool(ShellFileListItem);\r
1725 return (NULL);\r
1726 }\r
1727 }\r
1728 if (ShellFileListItem->FileName != NULL) {\r
1729 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
1730 TempString = StrnCatGrow(&TempString, &Size, ShellFileListItem->FileName, 0);\r
1731 if (TempString == NULL) {\r
1732 FreePool((VOID*)ShellFileListItem->FileName);\r
1733 FreePool(ShellFileListItem->Info);\r
1734 FreePool(ShellFileListItem);\r
1735 return (NULL);\r
1736 }\r
1737 }\r
1738\r
1739 ShellFileListItem->FullName = TempString;\r
1740 ShellFileListItem->Status = Status;\r
1741 ShellFileListItem->Handle = Handle;\r
1742\r
1743 return (ShellFileListItem);\r
1744}\r
1745\r
1746/**\r
1747 Find all files in a specified directory.\r
1748\r
1749 @param FileDirHandle Handle of the directory to search.\r
1750 @param FileList On return, points to the list of files in the directory\r
1751 or NULL if there are no files in the directory.\r
1752\r
1753 @retval EFI_SUCCESS File information was returned successfully.\r
1754 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.\r
1755 @retval EFI_DEVICE_ERROR The device reported an error.\r
1756 @retval EFI_NO_MEDIA The device media is not present.\r
1757 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.\r
1758 @return An error from FileHandleGetFileName().\r
1759**/\r
1760EFI_STATUS\r
1761EFIAPI\r
1762EfiShellFindFilesInDir(\r
1763 IN SHELL_FILE_HANDLE FileDirHandle,\r
1764 OUT EFI_SHELL_FILE_INFO **FileList\r
1765 )\r
1766{\r
1767 EFI_SHELL_FILE_INFO *ShellFileList;\r
1768 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1769 EFI_FILE_INFO *FileInfo;\r
1770 EFI_STATUS Status;\r
1771 BOOLEAN NoFile;\r
1772 CHAR16 *TempString;\r
1773 CHAR16 *BasePath;\r
1774 UINTN Size;\r
1775 CHAR16 *TempSpot;\r
1776\r
1777 Status = FileHandleGetFileName(FileDirHandle, &BasePath);\r
1778 if (EFI_ERROR(Status)) {\r
1779 return (Status);\r
1780 }\r
1781\r
1782 if (ShellFileHandleGetPath(FileDirHandle) != NULL) {\r
1783 TempString = NULL;\r
1784 Size = 0;\r
1785 TempString = StrnCatGrow(&TempString, &Size, ShellFileHandleGetPath(FileDirHandle), 0);\r
1786 TempSpot = StrStr(TempString, L";");\r
1787\r
1788 if (TempSpot != NULL) {\r
1789 *TempSpot = CHAR_NULL;\r
1790 }\r
1791\r
1792 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
1793 BasePath = TempString;\r
1794 }\r
1795\r
1796 NoFile = FALSE;\r
1797 ShellFileList = NULL;\r
1798 ShellFileListItem = NULL;\r
1799 FileInfo = NULL;\r
1800 Status = EFI_SUCCESS;\r
1801\r
1802\r
1803 for ( Status = FileHandleFindFirstFile(FileDirHandle, &FileInfo)\r
1804 ; !EFI_ERROR(Status) && !NoFile\r
1805 ; Status = FileHandleFindNextFile(FileDirHandle, FileInfo, &NoFile)\r
1806 ){\r
1807 TempString = NULL;\r
1808 Size = 0;\r
1809 //\r
1810 // allocate a new EFI_SHELL_FILE_INFO and populate it...\r
1811 //\r
1812 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
1813 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
1814 TempString = StrnCatGrow(&TempString, &Size, FileInfo->FileName, 0);\r
1815 ShellFileListItem = CreateAndPopulateShellFileInfo(\r
1816 BasePath,\r
1817 EFI_SUCCESS, // success since we didnt fail to open it...\r
1818 TempString,\r
1819 FileInfo->FileName,\r
1820 NULL, // no handle since not open\r
1821 FileInfo);\r
1822\r
1823 if (ShellFileList == NULL) {\r
1824 ShellFileList = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1825 ASSERT(ShellFileList != NULL);\r
1826 InitializeListHead(&ShellFileList->Link);\r
1827 }\r
1828 InsertTailList(&ShellFileList->Link, &ShellFileListItem->Link);\r
1829 }\r
1830 if (EFI_ERROR(Status)) {\r
1831 EfiShellFreeFileList(&ShellFileList);\r
1832 *FileList = NULL;\r
1833 } else {\r
1834 *FileList = ShellFileList;\r
1835 }\r
1836 SHELL_FREE_NON_NULL(BasePath);\r
1837 return(Status);\r
1838}\r
1839\r
1840/**\r
1841 Updates a file name to be preceeded by the mapped drive name\r
1842\r
1843 @param[in] BasePath the Mapped drive name to prepend\r
1844 @param[in,out] Path pointer to pointer to the file name to update.\r
1845\r
1846 @retval EFI_SUCCESS\r
1847 @retval EFI_OUT_OF_RESOURCES\r
1848**/\r
1849EFI_STATUS\r
1850EFIAPI\r
1851UpdateFileName(\r
1852 IN CONST CHAR16 *BasePath,\r
1853 IN OUT CHAR16 **Path\r
1854 )\r
1855{\r
1856 CHAR16 *Path2;\r
1857 UINTN Path2Size;\r
1858\r
1859 Path2Size = 0;\r
1860 Path2 = NULL;\r
1861\r
1862 ASSERT(Path != NULL);\r
1863 ASSERT(*Path != NULL);\r
1864 ASSERT(BasePath != NULL);\r
1865\r
1866 //\r
1867 // convert a local path to an absolute path\r
1868 //\r
1869 if (StrStr(*Path, L":") == NULL) {\r
1870 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
1871 StrnCatGrow(&Path2, &Path2Size, BasePath, 0);\r
1872 if (Path2 == NULL) {\r
1873 return (EFI_OUT_OF_RESOURCES);\r
1874 }\r
1875 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
1876 StrnCatGrow(&Path2, &Path2Size, (*Path)[0] == L'\\'?(*Path) + 1 :*Path, 0);\r
1877 if (Path2 == NULL) {\r
1878 return (EFI_OUT_OF_RESOURCES);\r
1879 }\r
1880 }\r
1881\r
1882 FreePool(*Path);\r
1883 (*Path) = Path2;\r
1884\r
1885 return (EFI_SUCCESS);\r
1886}\r
1887\r
1888/**\r
1889 If FileHandle is a directory then the function reads from FileHandle and reads in\r
1890 each of the FileInfo structures. If one of them matches the Pattern's first\r
1891 "level" then it opens that handle and calls itself on that handle.\r
1892\r
1893 If FileHandle is a file and matches all of the remaining Pattern (which would be\r
1894 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.\r
1895\r
a405b86d 1896 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call\r
1897 FreeFileList with FileList.\r
1898\r
1899 @param[in] FilePattern The FilePattern to check against.\r
1900 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure\r
1901 @param[in] FileHandle The FileHandle to start with\r
1902 @param[in,out] FileList pointer to pointer to list of found files.\r
1903 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.\r
8be0ba36 1904 @param[in] MapName The file system name this file is on.\r
a405b86d 1905\r
1906 @retval EFI_SUCCESS all files were found and the FileList contains a list.\r
1907 @retval EFI_NOT_FOUND no files were found\r
1908 @retval EFI_OUT_OF_RESOURCES a memory allocation failed\r
1909**/\r
1910EFI_STATUS\r
1911EFIAPI\r
1912ShellSearchHandle(\r
1913 IN CONST CHAR16 *FilePattern,\r
1914 IN EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation,\r
1915 IN SHELL_FILE_HANDLE FileHandle,\r
1916 IN OUT EFI_SHELL_FILE_INFO **FileList,\r
8be0ba36 1917 IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL,\r
1918 IN CONST CHAR16 *MapName\r
a405b86d 1919 )\r
1920{\r
1921 EFI_STATUS Status;\r
1922 CONST CHAR16 *NextFilePatternStart;\r
1923 CHAR16 *CurrentFilePattern;\r
1924 EFI_SHELL_FILE_INFO *ShellInfo;\r
1925 EFI_SHELL_FILE_INFO *ShellInfoNode;\r
1926 EFI_SHELL_FILE_INFO *NewShellNode;\r
1927 BOOLEAN Directory;\r
8be0ba36 1928 CHAR16 *NewFullName;\r
1929 UINTN Size;\r
a405b86d 1930\r
1931 if ( FilePattern == NULL\r
1932 || UnicodeCollation == NULL\r
1933 || FileList == NULL\r
1934 ){\r
1935 return (EFI_INVALID_PARAMETER);\r
1936 }\r
1937 ShellInfo = NULL;\r
1938 CurrentFilePattern = NULL;\r
1939\r
1940 if (*FilePattern == L'\\') {\r
1941 FilePattern++;\r
1942 }\r
1943\r
1944 for( NextFilePatternStart = FilePattern\r
1945 ; *NextFilePatternStart != CHAR_NULL && *NextFilePatternStart != L'\\'\r
1946 ; NextFilePatternStart++);\r
1947\r
1948 CurrentFilePattern = AllocateZeroPool((NextFilePatternStart-FilePattern+1)*sizeof(CHAR16));\r
1949 ASSERT(CurrentFilePattern != NULL);\r
1950 StrnCpy(CurrentFilePattern, FilePattern, NextFilePatternStart-FilePattern);\r
1951\r
1952 if (CurrentFilePattern[0] == CHAR_NULL\r
1953 &&NextFilePatternStart[0] == CHAR_NULL\r
1954 ){\r
1955 //\r
1956 // Add the current parameter FileHandle to the list, then end...\r
1957 //\r
1958 if (ParentNode == NULL) {\r
1959 Status = EFI_INVALID_PARAMETER;\r
1960 } else {\r
1961 NewShellNode = InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO*)ParentNode, TRUE);\r
1962 if (NewShellNode == NULL) {\r
1963 Status = EFI_OUT_OF_RESOURCES;\r
1964 } else {\r
1965 NewShellNode->Handle = NULL;\r
1966 if (*FileList == NULL) {\r
1967 *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));\r
1968 InitializeListHead(&((*FileList)->Link));\r
1969 }\r
1970\r
1971 //\r
1972 // Add to the returning to use list\r
1973 //\r
1974 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
1975\r
1976 Status = EFI_SUCCESS;\r
1977 }\r
1978 }\r
1979 } else {\r
1980 Status = EfiShellFindFilesInDir(FileHandle, &ShellInfo);\r
1981\r
1982 if (!EFI_ERROR(Status)){\r
1983 if (StrStr(NextFilePatternStart, L"\\") != NULL){\r
1984 Directory = TRUE;\r
1985 } else {\r
1986 Directory = FALSE;\r
1987 }\r
1988 for ( ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetFirstNode(&ShellInfo->Link)\r
1989 ; !IsNull (&ShellInfo->Link, &ShellInfoNode->Link)\r
1990 ; ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetNextNode(&ShellInfo->Link, &ShellInfoNode->Link)\r
1991 ){\r
1992 if (UnicodeCollation->MetaiMatch(UnicodeCollation, (CHAR16*)ShellInfoNode->FileName, CurrentFilePattern)){\r
8be0ba36 1993 if (ShellInfoNode->FullName != NULL && StrStr(ShellInfoNode->FullName, L":") == NULL) {\r
1994 Size = StrSize(ShellInfoNode->FullName);\r
1995 Size += StrSize(MapName) + sizeof(CHAR16);\r
1996 NewFullName = AllocateZeroPool(Size);\r
1997 if (NewFullName == NULL) {\r
1998 Status = EFI_OUT_OF_RESOURCES;\r
1999 } else {\r
2000 StrCpy(NewFullName, MapName);\r
2001 StrCat(NewFullName, ShellInfoNode->FullName+1);\r
2002 FreePool((VOID*)ShellInfoNode->FullName);\r
2003 ShellInfoNode->FullName = NewFullName;\r
2004 }\r
2005 }\r
c154b997 2006 if (Directory && !EFI_ERROR(Status) && ShellInfoNode->FullName != NULL && ShellInfoNode->FileName != NULL){\r
a405b86d 2007 //\r
2008 // should be a directory\r
2009 //\r
2010\r
2011 //\r
2012 // don't open the . and .. directories\r
2013 //\r
2014 if ( (StrCmp(ShellInfoNode->FileName, L".") != 0)\r
2015 && (StrCmp(ShellInfoNode->FileName, L"..") != 0)\r
2016 ){\r
2017 //\r
2018 //\r
2019 //\r
a405b86d 2020 if (EFI_ERROR(Status)) {\r
2021 break;\r
2022 }\r
2023 //\r
2024 // Open the directory since we need that handle in the next recursion.\r
2025 //\r
2026 ShellInfoNode->Status = EfiShellOpenFileByName (ShellInfoNode->FullName, &ShellInfoNode->Handle, EFI_FILE_MODE_READ);\r
2027\r
2028 //\r
2029 // recurse with the next part of the pattern\r
2030 //\r
8be0ba36 2031 Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode, MapName);\r
a405b86d 2032 }\r
8be0ba36 2033 } else if (!EFI_ERROR(Status)) {\r
a405b86d 2034 //\r
2035 // should be a file\r
2036 //\r
2037\r
2038 //\r
2039 // copy the information we need into a new Node\r
2040 //\r
2041 NewShellNode = InternalDuplicateShellFileInfo(ShellInfoNode, FALSE);\r
2042 ASSERT(NewShellNode != NULL);\r
2043 if (NewShellNode == NULL) {\r
2044 Status = EFI_OUT_OF_RESOURCES;\r
2045 }\r
2046 if (*FileList == NULL) {\r
2047 *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));\r
2048 InitializeListHead(&((*FileList)->Link));\r
2049 }\r
2050\r
2051 //\r
2052 // Add to the returning to use list\r
2053 //\r
2054 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
2055 }\r
2056 }\r
2057 if (EFI_ERROR(Status)) {\r
2058 break;\r
2059 }\r
2060 }\r
2061 if (EFI_ERROR(Status)) {\r
2062 EfiShellFreeFileList(&ShellInfo);\r
2063 } else {\r
2064 Status = EfiShellFreeFileList(&ShellInfo);\r
2065 }\r
2066 }\r
2067 }\r
2068\r
2069 FreePool(CurrentFilePattern);\r
2070 return (Status);\r
2071}\r
2072\r
2073/**\r
2074 Find files that match a specified pattern.\r
2075\r
2076 This function searches for all files and directories that match the specified\r
2077 FilePattern. The FilePattern can contain wild-card characters. The resulting file\r
2078 information is placed in the file list FileList.\r
2079\r
2080 Wildcards are processed\r
2081 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.\r
2082\r
2083 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo\r
2084 field is set to NULL.\r
2085\r
2086 if *FileList is not NULL then it must be a pre-existing and properly initialized list.\r
2087\r
2088 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.\r
2089 @param FileList On return, points to the start of a file list containing the names\r
2090 of all matching files or else points to NULL if no matching files\r
2091 were found. only on a EFI_SUCCESS return will; this be non-NULL.\r
2092\r
2093 @retval EFI_SUCCESS Files found. FileList is a valid list.\r
2094 @retval EFI_NOT_FOUND No files found.\r
2095 @retval EFI_NO_MEDIA The device has no media\r
2096 @retval EFI_DEVICE_ERROR The device reported an error\r
2097 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted\r
2098**/\r
2099EFI_STATUS\r
2100EFIAPI\r
2101EfiShellFindFiles(\r
2102 IN CONST CHAR16 *FilePattern,\r
2103 OUT EFI_SHELL_FILE_INFO **FileList\r
2104 )\r
2105{\r
2106 EFI_STATUS Status;\r
2107 CHAR16 *PatternCopy;\r
2108 CHAR16 *PatternCurrentLocation;\r
2109 EFI_DEVICE_PATH_PROTOCOL *RootDevicePath;\r
2110 SHELL_FILE_HANDLE RootFileHandle;\r
2111 CHAR16 *MapName;\r
2112 UINTN Count;\r
2113\r
2114 if ( FilePattern == NULL\r
2115 || FileList == NULL\r
2116 || StrStr(FilePattern, L":") == NULL\r
2117 ){\r
2118 return (EFI_INVALID_PARAMETER);\r
2119 }\r
2120 Status = EFI_SUCCESS;\r
2121 RootDevicePath = NULL;\r
2122 RootFileHandle = NULL;\r
2123 MapName = NULL;\r
2124 PatternCopy = AllocatePool(StrSize(FilePattern));\r
2125 if (PatternCopy == NULL) {\r
2126 return (EFI_OUT_OF_RESOURCES);\r
2127 }\r
2128 StrCpy(PatternCopy, FilePattern);\r
2129\r
2130 PatternCopy = CleanPath(PatternCopy);\r
2131\r
2132 Count = StrStr(PatternCopy, L":") - PatternCopy;\r
2133 Count += 2;\r
2134\r
2135 ASSERT(MapName == NULL);\r
2136 MapName = StrnCatGrow(&MapName, NULL, PatternCopy, Count);\r
2137\r
2138 if (!EFI_ERROR(Status)) {\r
2139 RootDevicePath = EfiShellGetDevicePathFromFilePath(PatternCopy);\r
2140 if (RootDevicePath == NULL) {\r
2141 Status = EFI_INVALID_PARAMETER;\r
2142 } else {\r
2143 Status = EfiShellOpenRoot(RootDevicePath, &RootFileHandle);\r
2144 if (!EFI_ERROR(Status)) {\r
2145 for ( PatternCurrentLocation = PatternCopy\r
2146 ; *PatternCurrentLocation != ':'\r
2147 ; PatternCurrentLocation++);\r
2148 PatternCurrentLocation++;\r
8be0ba36 2149 Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL, MapName);\r
a405b86d 2150 }\r
2151 FreePool(RootDevicePath);\r
2152 }\r
2153 }\r
2154\r
8be0ba36 2155 SHELL_FREE_NON_NULL(PatternCopy);\r
2156 SHELL_FREE_NON_NULL(MapName);\r
a405b86d 2157\r
2158 return(Status);\r
2159}\r
2160\r
2161/**\r
2162 Opens the files that match the path specified.\r
2163\r
2164 This function opens all of the files specified by Path. Wildcards are processed\r
2165 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each\r
2166 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.\r
2167\r
2168 @param Path A pointer to the path string.\r
2169 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or\r
2170 EFI_FILE_MODE_WRITE.\r
2171 @param FileList Points to the start of a list of files opened.\r
2172\r
2173 @retval EFI_SUCCESS Create the file list successfully.\r
2174 @return Others Can't create the file list.\r
2175**/\r
2176EFI_STATUS\r
2177EFIAPI\r
2178EfiShellOpenFileList(\r
2179 IN CHAR16 *Path,\r
2180 IN UINT64 OpenMode,\r
2181 IN OUT EFI_SHELL_FILE_INFO **FileList\r
2182 )\r
2183{\r
2184 EFI_STATUS Status;\r
2185 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
2186 CHAR16 *Path2;\r
2187 UINTN Path2Size;\r
2188 CONST CHAR16 *CurDir;\r
2189\r
2190 ShellCommandCleanPath(Path);\r
2191\r
2192 Path2Size = 0;\r
2193 Path2 = NULL;\r
2194\r
2195 ASSERT(FileList != NULL);\r
2196 ASSERT(*FileList != NULL);\r
2197\r
2198 if (*Path == L'.' && *(Path+1) == L'\\') {\r
2199 Path++;\r
2200 }\r
2201\r
2202 //\r
2203 // convert a local path to an absolute path\r
2204 //\r
2205 if (StrStr(Path, L":") == NULL) {\r
2206 CurDir = EfiShellGetCurDir(NULL);\r
2207 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
2208 StrnCatGrow(&Path2, &Path2Size, CurDir, 0);\r
2209 if (*Path == L'\\') {\r
2210 Path++;\r
2211 }\r
2212 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
2213 StrnCatGrow(&Path2, &Path2Size, Path, 0);\r
2214 } else {\r
2215 ASSERT(Path2 == NULL);\r
2216 StrnCatGrow(&Path2, NULL, Path, 0);\r
2217 }\r
2218\r
2219 CleanPath (Path2);\r
2220\r
2221 //\r
2222 // do the search\r
2223 //\r
2224 Status = EfiShellFindFiles(Path2, FileList);\r
2225\r
2226 FreePool(Path2);\r
2227\r
2228 if (EFI_ERROR(Status)) {\r
2229 return (Status);\r
2230 }\r
2231\r
2232 //\r
2233 // We had no errors so open all the files (that are not already opened...)\r
2234 //\r
2235 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
2236 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)\r
2237 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
2238 ){\r
2239 if (ShellFileListItem->Status == 0 && ShellFileListItem->Handle == NULL) {\r
2240 ShellFileListItem->Status = EfiShellOpenFileByName (ShellFileListItem->FullName, &ShellFileListItem->Handle, OpenMode);\r
2241 }\r
2242 }\r
2243\r
2244 return(EFI_SUCCESS);\r
2245}\r
2246\r
2247/**\r
2248 This function updated with errata.\r
2249\r
2250 Gets either a single or list of environment variables.\r
2251\r
2252 If name is not NULL then this function returns the current value of the specified\r
2253 environment variable.\r
2254\r
2255 If Name is NULL, then a list of all environment variable names is returned. Each is a\r
2256 NULL terminated string with a double NULL terminating the list.\r
2257\r
2258 @param Name A pointer to the environment variable name. If\r
2259 Name is NULL, then the function will return all\r
2260 of the defined shell environment variables. In\r
2261 the case where multiple environment variables are\r
2262 being returned, each variable will be terminated by\r
2263 a NULL, and the list will be terminated by a double\r
2264 NULL.\r
2265\r
2266 @return !=NULL A pointer to the returned string.\r
2267 The returned pointer does not need to be freed by the caller.\r
2268\r
2269 @retval NULL The environment variable doesn't exist or there are\r
2270 no environment variables.\r
2271**/\r
2272CONST CHAR16 *\r
2273EFIAPI\r
2274EfiShellGetEnv(\r
2275 IN CONST CHAR16 *Name\r
2276 )\r
2277{\r
2278 EFI_STATUS Status;\r
2279 VOID *Buffer;\r
2280 UINTN Size;\r
2281 LIST_ENTRY List;\r
2282 ENV_VAR_LIST *Node;\r
2283 CHAR16 *CurrentWriteLocation;\r
2284\r
2285 Size = 0;\r
2286 Buffer = NULL;\r
2287\r
2288 if (Name == NULL) {\r
2289 //\r
2290 // Get all our environment variables\r
2291 //\r
2292 InitializeListHead(&List);\r
2293 Status = GetEnvironmentVariableList(&List);\r
2294 if (EFI_ERROR(Status)){\r
2295 return (NULL);\r
2296 }\r
2297\r
2298 //\r
2299 // Build the semi-colon delimited list. (2 passes)\r
2300 //\r
2301 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)\r
2302 ; !IsNull(&List, &Node->Link)\r
2303 ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
2304 ){\r
2305 ASSERT(Node->Key != NULL);\r
2306 Size += StrSize(Node->Key);\r
2307 }\r
2308\r
2309 Size += 2*sizeof(CHAR16);\r
2310\r
2311 Buffer = AllocateZeroPool(Size);\r
3c865f20 2312 if (Buffer == NULL) {\r
2313 if (!IsListEmpty (&List)) {\r
2314 FreeEnvironmentVariableList(&List);\r
2315 }\r
2316 return (NULL);\r
2317 }\r
a405b86d 2318 CurrentWriteLocation = (CHAR16*)Buffer;\r
2319\r
2320 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)\r
2321 ; !IsNull(&List, &Node->Link)\r
2322 ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
2323 ){\r
2324 ASSERT(Node->Key != NULL);\r
2325 StrCpy(CurrentWriteLocation, Node->Key);\r
2326 CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
2327 }\r
2328\r
2329 //\r
2330 // Free the list...\r
2331 //\r
3c865f20 2332 if (!IsListEmpty (&List)) {\r
2333 FreeEnvironmentVariableList(&List);\r
2334 }\r
a405b86d 2335 } else {\r
2336 //\r
2337 // We are doing a specific environment variable\r
2338 //\r
2339\r
2340 //\r
2341 // get the size we need for this EnvVariable\r
2342 //\r
2343 Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, Buffer);\r
2344 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2345 //\r
2346 // Allocate the space and recall the get function\r
2347 //\r
2348 Buffer = AllocateZeroPool(Size);\r
2349 ASSERT(Buffer != NULL);\r
2350 Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, Buffer);\r
2351 }\r
2352 //\r
2353 // we didnt get it (might not exist)\r
2354 // free the memory if we allocated any and return NULL\r
2355 //\r
2356 if (EFI_ERROR(Status)) {\r
2357 if (Buffer != NULL) {\r
2358 FreePool(Buffer);\r
2359 }\r
2360 return (NULL);\r
2361 }\r
2362 }\r
2363\r
2364 //\r
2365 // return the buffer\r
2366 //\r
2367 return (AddBufferToFreeList(Buffer));\r
2368}\r
2369\r
2370/**\r
2371 Internal variable setting function. Allows for setting of the read only variables.\r
2372\r
2373 @param Name Points to the NULL-terminated environment variable name.\r
2374 @param Value Points to the NULL-terminated environment variable value. If the value is an\r
2375 empty string then the environment variable is deleted.\r
2376 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
2377\r
2378 @retval EFI_SUCCESS The environment variable was successfully updated.\r
2379**/\r
2380EFI_STATUS\r
2381EFIAPI\r
2382InternalEfiShellSetEnv(\r
2383 IN CONST CHAR16 *Name,\r
2384 IN CONST CHAR16 *Value,\r
2385 IN BOOLEAN Volatile\r
2386 )\r
2387{\r
2388 if (Value == NULL || StrLen(Value) == 0) {\r
2389 return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name));\r
2390 } else {\r
2391 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
2392 if (Volatile) {\r
2393 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name, StrSize(Value), Value));\r
2394 } else {\r
2395 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name, StrSize(Value), Value));\r
2396 }\r
2397 }\r
2398}\r
2399\r
2400/**\r
2401 Sets the environment variable.\r
2402\r
2403 This function changes the current value of the specified environment variable. If the\r
2404 environment variable exists and the Value is an empty string, then the environment\r
2405 variable is deleted. If the environment variable exists and the Value is not an empty\r
2406 string, then the value of the environment variable is changed. If the environment\r
2407 variable does not exist and the Value is an empty string, there is no action. If the\r
2408 environment variable does not exist and the Value is a non-empty string, then the\r
2409 environment variable is created and assigned the specified value.\r
2410\r
2411 For a description of volatile and non-volatile environment variables, see UEFI Shell\r
2412 2.0 specification section 3.6.1.\r
2413\r
2414 @param Name Points to the NULL-terminated environment variable name.\r
2415 @param Value Points to the NULL-terminated environment variable value. If the value is an\r
2416 empty string then the environment variable is deleted.\r
2417 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
2418\r
2419 @retval EFI_SUCCESS The environment variable was successfully updated.\r
2420**/\r
2421EFI_STATUS\r
2422EFIAPI\r
2423EfiShellSetEnv(\r
2424 IN CONST CHAR16 *Name,\r
2425 IN CONST CHAR16 *Value,\r
2426 IN BOOLEAN Volatile\r
2427 )\r
2428{\r
2429 if (Name == NULL || *Name == CHAR_NULL) {\r
2430 return (EFI_INVALID_PARAMETER);\r
2431 }\r
2432 //\r
2433 // Make sure we dont 'set' a predefined read only variable\r
2434 //\r
2435 if (gUnicodeCollation->StriColl(\r
2436 gUnicodeCollation,\r
2437 (CHAR16*)Name,\r
2438 L"cwd") == 0\r
2439 ||gUnicodeCollation->StriColl(\r
2440 gUnicodeCollation,\r
2441 (CHAR16*)Name,\r
2442 L"Lasterror") == 0\r
2443 ||gUnicodeCollation->StriColl(\r
2444 gUnicodeCollation,\r
2445 (CHAR16*)Name,\r
2446 L"profiles") == 0\r
2447 ||gUnicodeCollation->StriColl(\r
2448 gUnicodeCollation,\r
2449 (CHAR16*)Name,\r
2450 L"uefishellsupport") == 0\r
2451 ||gUnicodeCollation->StriColl(\r
2452 gUnicodeCollation,\r
2453 (CHAR16*)Name,\r
2454 L"uefishellversion") == 0\r
2455 ||gUnicodeCollation->StriColl(\r
2456 gUnicodeCollation,\r
2457 (CHAR16*)Name,\r
2458 L"uefiversion") == 0\r
2459 ){\r
2460 return (EFI_INVALID_PARAMETER);\r
2461 }\r
2462 return (InternalEfiShellSetEnv(Name, Value, Volatile));\r
2463}\r
2464\r
2465/**\r
2466 Returns the current directory on the specified device.\r
2467\r
2468 If FileSystemMapping is NULL, it returns the current working directory. If the\r
2469 FileSystemMapping is not NULL, it returns the current directory associated with the\r
2470 FileSystemMapping. In both cases, the returned name includes the file system\r
2471 mapping (i.e. fs0:\current-dir).\r
2472\r
2473 @param FileSystemMapping A pointer to the file system mapping. If NULL,\r
2474 then the current working directory is returned.\r
2475\r
2476 @retval !=NULL The current directory.\r
2477 @retval NULL Current directory does not exist.\r
2478**/\r
2479CONST CHAR16 *\r
2480EFIAPI\r
2481EfiShellGetCurDir(\r
2482 IN CONST CHAR16 *FileSystemMapping OPTIONAL\r
2483 )\r
2484{\r
2485 CHAR16 *PathToReturn;\r
2486 UINTN Size;\r
2487 SHELL_MAP_LIST *MapListItem;\r
2488 if (!IsListEmpty(&gShellMapList.Link)) {\r
2489 //\r
2490 // if parameter is NULL, use current\r
2491 //\r
2492 if (FileSystemMapping == NULL) {\r
2493 return (EfiShellGetEnv(L"cwd"));\r
2494 } else {\r
2495 Size = 0;\r
2496 PathToReturn = NULL;\r
2497 MapListItem = ShellCommandFindMapItem(FileSystemMapping);\r
2498 if (MapListItem != NULL) {\r
2499 ASSERT((PathToReturn == NULL && Size == 0) || (PathToReturn != NULL));\r
2500 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->MapName, 0);\r
2501 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->CurrentDirectoryPath, 0);\r
2502 }\r
2503 }\r
2504 return (AddBufferToFreeList(PathToReturn));\r
2505 } else {\r
2506 return (NULL);\r
2507 }\r
2508}\r
2509\r
2510/**\r
2511 Changes the current directory on the specified device.\r
2512\r
2513 If the FileSystem is NULL, and the directory Dir does not contain a file system's\r
2514 mapped name, this function changes the current working directory.\r
2515\r
2516 If the FileSystem is NULL and the directory Dir contains a mapped name, then the\r
2517 current file system and the current directory on that file system are changed.\r
2518\r
2519 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file\r
2520 system.\r
2521\r
2522 If FileSystem is not NULL and Dir is not NULL, then this function changes the current\r
2523 directory on the specified file system.\r
2524\r
2525 If the current working directory or the current working file system is changed then the\r
2526 %cwd% environment variable will be updated\r
2527\r
2528 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working\r
2529 directory is changed.\r
2530 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.\r
2531\r
2532 @retval EFI_SUCCESS The operation was sucessful\r
2533 @retval EFI_NOT_FOUND The file system could not be found\r
2534**/\r
2535EFI_STATUS\r
2536EFIAPI\r
2537EfiShellSetCurDir(\r
2538 IN CONST CHAR16 *FileSystem OPTIONAL,\r
2539 IN CONST CHAR16 *Dir\r
2540 )\r
2541{\r
2542 CHAR16 *MapName;\r
2543 SHELL_MAP_LIST *MapListItem;\r
2544 UINTN Size;\r
2545 EFI_STATUS Status;\r
2546 CHAR16 *TempString;\r
2547 CHAR16 *DirectoryName;\r
2548 UINTN TempLen;\r
2549\r
2550 Size = 0;\r
2551 MapName = NULL;\r
2552 MapListItem = NULL;\r
2553 TempString = NULL;\r
2554 DirectoryName = NULL;\r
2555\r
3c865f20 2556 if ((FileSystem == NULL && Dir == NULL) || Dir == NULL) {\r
a405b86d 2557 return (EFI_INVALID_PARAMETER);\r
2558 }\r
2559\r
2560 if (IsListEmpty(&gShellMapList.Link)){\r
2561 return (EFI_NOT_FOUND);\r
2562 }\r
2563\r
2564 DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0);\r
2565 ASSERT(DirectoryName != NULL);\r
2566\r
2567 CleanPath(DirectoryName);\r
2568\r
2569 if (FileSystem == NULL) {\r
2570 //\r
2571 // determine the file system mapping to use\r
2572 //\r
2573 if (StrStr(DirectoryName, L":") != NULL) {\r
2574 ASSERT(MapName == NULL);\r
2575 MapName = StrnCatGrow(&MapName, NULL, DirectoryName, (StrStr(DirectoryName, L":")-DirectoryName+1));\r
2576 }\r
2577 //\r
2578 // find the file system mapping's entry in the list\r
2579 // or use current\r
2580 //\r
2581 if (MapName != NULL) {\r
2582 MapListItem = ShellCommandFindMapItem(MapName);\r
2583\r
2584 //\r
2585 // make that the current file system mapping\r
2586 //\r
2587 if (MapListItem != NULL) {\r
2588 gShellCurDir = MapListItem;\r
2589 }\r
2590 } else {\r
2591 MapListItem = gShellCurDir;\r
2592 }\r
2593\r
2594 if (MapListItem == NULL) {\r
2595 return (EFI_NOT_FOUND);\r
2596 }\r
2597\r
2598 //\r
2599 // now update the MapListItem's current directory\r
2600 //\r
2601 if (MapListItem->CurrentDirectoryPath != NULL && DirectoryName[StrLen(DirectoryName) - 1] != L':') {\r
2602 FreePool(MapListItem->CurrentDirectoryPath);\r
2603 MapListItem->CurrentDirectoryPath = NULL;\r
2604 }\r
2605 if (MapName != NULL) {\r
2606 TempLen = StrLen(MapName);\r
2607 if (TempLen != StrLen(DirectoryName)) {\r
2608 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2609 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName+StrLen(MapName), 0);\r
2610 }\r
2611 } else {\r
2612 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2613 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
2614 }\r
2615 if ((MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') || (MapListItem->CurrentDirectoryPath == NULL)) {\r
2616 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2617 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
2618 }\r
2619 } else {\r
2620 //\r
2621 // cant have a mapping in the directory...\r
2622 //\r
2623 if (StrStr(DirectoryName, L":") != NULL) {\r
2624 return (EFI_INVALID_PARAMETER);\r
2625 }\r
2626 //\r
2627 // FileSystem != NULL\r
2628 //\r
2629 MapListItem = ShellCommandFindMapItem(FileSystem);\r
2630 if (MapListItem == NULL) {\r
2631 return (EFI_INVALID_PARAMETER);\r
2632 }\r
2633// gShellCurDir = MapListItem;\r
2634 if (DirectoryName != NULL) {\r
2635 //\r
2636 // change current dir on that file system\r
2637 //\r
2638\r
2639 if (MapListItem->CurrentDirectoryPath != NULL) {\r
2640 FreePool(MapListItem->CurrentDirectoryPath);\r
2641 DEBUG_CODE(MapListItem->CurrentDirectoryPath = NULL;);\r
2642 }\r
2643// ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2644// MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);\r
2645 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2646 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
2647 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2648 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
2649 if (MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') {\r
2650 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
2651 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
2652 }\r
2653 }\r
2654 }\r
2655 //\r
2656 // if updated the current directory then update the environment variable\r
2657 //\r
2658 if (MapListItem == gShellCurDir) {\r
2659 Size = 0;\r
2660 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
2661 StrnCatGrow(&TempString, &Size, MapListItem->MapName, 0);\r
2662 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
2663 StrnCatGrow(&TempString, &Size, MapListItem->CurrentDirectoryPath, 0);\r
2664 Status = InternalEfiShellSetEnv(L"cwd", TempString, TRUE);\r
2665 FreePool(TempString);\r
2666 return (Status);\r
2667 }\r
2668 return(EFI_SUCCESS);\r
2669}\r
2670\r
2671/**\r
2672 Return help information about a specific command.\r
2673\r
2674 This function returns the help information for the specified command. The help text\r
2675 can be internal to the shell or can be from a UEFI Shell manual page.\r
2676\r
2677 If Sections is specified, then each section name listed will be compared in a casesensitive\r
2678 manner, to the section names described in Appendix B. If the section exists,\r
2679 it will be appended to the returned help text. If the section does not exist, no\r
2680 information will be returned. If Sections is NULL, then all help text information\r
2681 available will be returned.\r
2682\r
2683 @param Command Points to the NULL-terminated UEFI Shell command name.\r
2684 @param Sections Points to the NULL-terminated comma-delimited\r
2685 section names to return. If NULL, then all\r
2686 sections will be returned.\r
2687 @param HelpText On return, points to a callee-allocated buffer\r
2688 containing all specified help text.\r
2689\r
2690 @retval EFI_SUCCESS The help text was returned.\r
2691 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the\r
2692 returned help text.\r
2693 @retval EFI_INVALID_PARAMETER HelpText is NULL\r
2694 @retval EFI_NOT_FOUND There is no help text available for Command.\r
2695**/\r
2696EFI_STATUS\r
2697EFIAPI\r
2698EfiShellGetHelpText(\r
2699 IN CONST CHAR16 *Command,\r
2700 IN CONST CHAR16 *Sections OPTIONAL,\r
2701 OUT CHAR16 **HelpText\r
2702 )\r
2703{\r
2704 CONST CHAR16 *ManFileName;\r
2705\r
2706 ASSERT(HelpText != NULL);\r
2707\r
2708 ManFileName = ShellCommandGetManFileNameHandler(Command);\r
2709\r
2710 if (ManFileName != NULL) {\r
2711 return (ProcessManFile(ManFileName, Command, Sections, NULL, HelpText));\r
2712 } else {\r
2713 return (ProcessManFile(Command, Command, Sections, NULL, HelpText));\r
2714 }\r
2715}\r
2716\r
2717/**\r
2718 Gets the enable status of the page break output mode.\r
2719\r
2720 User can use this function to determine current page break mode.\r
2721\r
2722 @retval TRUE The page break output mode is enabled.\r
2723 @retval FALSE The page break output mode is disabled.\r
2724**/\r
2725BOOLEAN\r
2726EFIAPI\r
2727EfiShellGetPageBreak(\r
2728 VOID\r
2729 )\r
2730{\r
2731 return(ShellInfoObject.PageBreakEnabled);\r
2732}\r
2733\r
2734/**\r
2735 Judges whether the active shell is the root shell.\r
2736\r
2737 This function makes the user to know that whether the active Shell is the root shell.\r
2738\r
2739 @retval TRUE The active Shell is the root Shell.\r
2740 @retval FALSE The active Shell is NOT the root Shell.\r
2741**/\r
2742BOOLEAN\r
2743EFIAPI\r
2744EfiShellIsRootShell(\r
2745 VOID\r
2746 )\r
2747{\r
2748 return(ShellInfoObject.RootShellInstance);\r
2749}\r
2750\r
2751/**\r
2752 function to return a semi-colon delimeted list of all alias' in the current shell\r
2753\r
2754 up to caller to free the memory.\r
2755\r
2756 @retval NULL No alias' were found\r
2757 @retval NULL An error ocurred getting alias'\r
2758 @return !NULL a list of all alias'\r
2759**/\r
2760CHAR16 *\r
2761EFIAPI\r
2762InternalEfiShellGetListAlias(\r
2763 )\r
2764{\r
2765 UINT64 MaxStorSize;\r
2766 UINT64 RemStorSize;\r
2767 UINT64 MaxVarSize;\r
2768 EFI_STATUS Status;\r
2769 EFI_GUID Guid;\r
2770 CHAR16 *VariableName;\r
2771 UINTN NameSize;\r
2772 CHAR16 *RetVal;\r
2773 UINTN RetSize;\r
2774 CHAR16 *Alias;\r
2775\r
2776 Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize);\r
2777 ASSERT_EFI_ERROR(Status);\r
2778\r
2779 VariableName = AllocateZeroPool((UINTN)MaxVarSize);\r
2780 RetSize = 0;\r
2781 RetVal = NULL;\r
2782\r
3c865f20 2783 if (VariableName == NULL) {\r
2784 return (NULL);\r
2785 }\r
2786\r
a405b86d 2787 VariableName[0] = CHAR_NULL;\r
2788\r
2789 while (TRUE) {\r
2790 NameSize = (UINTN)MaxVarSize;\r
2791 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
2792 if (Status == EFI_NOT_FOUND){\r
2793 break;\r
2794 }\r
2795 ASSERT_EFI_ERROR(Status);\r
2796 if (EFI_ERROR(Status)) {\r
2797 break;\r
2798 }\r
2799 if (CompareGuid(&Guid, &gShellAliasGuid)){\r
2800 Alias = GetVariable(VariableName, &gShellAliasGuid);\r
2801 ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL));\r
2802 RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0);\r
2803 RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0);\r
2804 } // compare guid\r
2805 } // while\r
2806 FreePool(VariableName);\r
2807\r
2808 return (RetVal);\r
2809}\r
2810\r
2811/**\r
2812 This function returns the command associated with a alias or a list of all\r
2813 alias'.\r
2814\r
2815 @param[in] Alias Points to the NULL-terminated shell alias.\r
2816 If this parameter is NULL, then all\r
2817 aliases will be returned in ReturnedData.\r
2818 @param[out] Volatile upon return of a single command if TRUE indicates\r
2819 this is stored in a volatile fashion. FALSE otherwise.\r
2820\r
2821 @return If Alias is not NULL, it will return a pointer to\r
2822 the NULL-terminated command for that alias.\r
2823 If Alias is NULL, ReturnedData points to a ';'\r
2824 delimited list of alias (e.g.\r
2825 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.\r
2826 @retval NULL an error ocurred\r
2827 @retval NULL Alias was not a valid Alias\r
2828**/\r
2829CONST CHAR16 *\r
2830EFIAPI\r
2831EfiShellGetAlias(\r
2832 IN CONST CHAR16 *Alias,\r
2833 OUT BOOLEAN *Volatile OPTIONAL\r
2834 )\r
2835{\r
2836 CHAR16 *RetVal;\r
2837 UINTN RetSize;\r
2838 UINT32 Attribs;\r
2839 EFI_STATUS Status;\r
2840\r
2841 if (Alias != NULL) {\r
2842 if (Volatile == NULL) {\r
2843 return (AddBufferToFreeList(GetVariable((CHAR16*)Alias, &gShellAliasGuid)));\r
2844 }\r
2845 RetSize = 0;\r
2846 RetVal = NULL;\r
2847 Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
2848 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2849 RetVal = AllocateZeroPool(RetSize);\r
2850 Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
2851 }\r
2852 if (EFI_ERROR(Status)) {\r
2853 if (RetVal != NULL) {\r
2854 FreePool(RetVal);\r
2855 }\r
2856 return (NULL);\r
2857 }\r
2858 if ((EFI_VARIABLE_NON_VOLATILE & Attribs) == EFI_VARIABLE_NON_VOLATILE) {\r
2859 *Volatile = FALSE;\r
2860 } else {\r
2861 *Volatile = TRUE;\r
2862 }\r
2863\r
2864 return (AddBufferToFreeList(RetVal));\r
2865 }\r
2866 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));\r
2867}\r
2868\r
2869/**\r
2870 Changes a shell command alias.\r
2871\r
2872 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.\r
2873\r
2874 this function does not check for built in alias'.\r
2875\r
2876 @param[in] Command Points to the NULL-terminated shell command or existing alias.\r
2877 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and\r
2878 Command refers to an alias, that alias will be deleted.\r
2879 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the\r
2880 Alias being set will be stored in a non-volatile fashion.\r
2881\r
2882 @retval EFI_SUCCESS Alias created or deleted successfully.\r
2883 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found\r
2884**/\r
2885EFI_STATUS\r
2886EFIAPI\r
2887InternalSetAlias(\r
2888 IN CONST CHAR16 *Command,\r
2889 IN CONST CHAR16 *Alias,\r
2890 IN BOOLEAN Volatile\r
2891 )\r
2892{\r
2893 //\r
2894 // We must be trying to remove one if Alias is NULL\r
2895 //\r
2896 if (Alias == NULL) {\r
2897 //\r
2898 // remove an alias (but passed in COMMAND parameter)\r
2899 //\r
2900 return (gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL));\r
2901 } else {\r
2902 //\r
2903 // Add and replace are the same\r
2904 //\r
2905\r
2906 // We dont check the error return on purpose since the variable may not exist.\r
2907 gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL);\r
2908\r
2909 return (gRT->SetVariable((CHAR16*)Alias, &gShellAliasGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS|(Volatile?0:EFI_VARIABLE_NON_VOLATILE), StrSize(Command), (VOID*)Command));\r
2910 }\r
2911}\r
2912\r
2913/**\r
2914 Changes a shell command alias.\r
2915\r
2916 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.\r
2917\r
2918\r
2919 @param[in] Command Points to the NULL-terminated shell command or existing alias.\r
2920 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and\r
2921 Command refers to an alias, that alias will be deleted.\r
2922 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If\r
2923 FALSE and the alias already exists, then the existing alias is unchanged and\r
2924 EFI_ACCESS_DENIED is returned.\r
2925 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the\r
2926 Alias being set will be stored in a non-volatile fashion.\r
2927\r
2928 @retval EFI_SUCCESS Alias created or deleted successfully.\r
2929 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found\r
2930 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to\r
2931 FALSE.\r
2932**/\r
2933EFI_STATUS\r
2934EFIAPI\r
2935EfiShellSetAlias(\r
2936 IN CONST CHAR16 *Command,\r
2937 IN CONST CHAR16 *Alias,\r
2938 IN BOOLEAN Replace,\r
2939 IN BOOLEAN Volatile\r
2940 )\r
2941{\r
2942 //\r
2943 // cant set over a built in alias\r
2944 //\r
2945 if (ShellCommandIsOnAliasList(Alias==NULL?Command:Alias)) {\r
2946 return (EFI_ACCESS_DENIED);\r
2947 }\r
2948 if (Command == NULL || *Command == CHAR_NULL || StrLen(Command) == 0) {\r
2949 return (EFI_INVALID_PARAMETER);\r
2950 }\r
2951\r
2952 if (EfiShellGetAlias(Command, NULL) != NULL && !Replace) {\r
2953 return (EFI_ACCESS_DENIED);\r
2954 }\r
2955\r
2956 return (InternalSetAlias(Command, Alias, Volatile));\r
2957}\r
2958\r
2959// Pure FILE_HANDLE operations are passed to FileHandleLib\r
2960// these functions are indicated by the *\r
2961EFI_SHELL_PROTOCOL mShellProtocol = {\r
2962 EfiShellExecute,\r
2963 EfiShellGetEnv,\r
2964 EfiShellSetEnv,\r
2965 EfiShellGetAlias,\r
2966 EfiShellSetAlias,\r
2967 EfiShellGetHelpText,\r
2968 EfiShellGetDevicePathFromMap,\r
2969 EfiShellGetMapFromDevicePath,\r
2970 EfiShellGetDevicePathFromFilePath,\r
2971 EfiShellGetFilePathFromDevicePath,\r
2972 EfiShellSetMap,\r
2973 EfiShellGetCurDir,\r
2974 EfiShellSetCurDir,\r
2975 EfiShellOpenFileList,\r
2976 EfiShellFreeFileList,\r
2977 EfiShellRemoveDupInFileList,\r
2978 EfiShellBatchIsActive,\r
2979 EfiShellIsRootShell,\r
2980 EfiShellEnablePageBreak,\r
2981 EfiShellDisablePageBreak,\r
2982 EfiShellGetPageBreak,\r
2983 EfiShellGetDeviceName,\r
2984 (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo, //*\r
2985 (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo, //*\r
2986 EfiShellOpenFileByName,\r
2987 EfiShellClose,\r
2988 EfiShellCreateFile,\r
2989 (EFI_SHELL_READ_FILE)FileHandleRead, //*\r
2990 (EFI_SHELL_WRITE_FILE)FileHandleWrite, //*\r
2991 (EFI_SHELL_DELETE_FILE)FileHandleDelete, //*\r
2992 EfiShellDeleteFileByName,\r
2993 (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition, //*\r
2994 (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition, //*\r
2995 (EFI_SHELL_FLUSH_FILE)FileHandleFlush, //*\r
2996 EfiShellFindFiles,\r
2997 EfiShellFindFilesInDir,\r
2998 (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize, //*\r
2999 EfiShellOpenRoot,\r
3000 EfiShellOpenRootByHandle,\r
3001 NULL,\r
3002 SHELL_MAJOR_VERSION,\r
3003 SHELL_MINOR_VERSION\r
3004};\r
3005\r
3006/**\r
3007 Function to create and install on the current handle.\r
3008\r
3009 Will overwrite any existing ShellProtocols in the system to be sure that\r
3010 the current shell is in control.\r
3011\r
3012 This must be removed via calling CleanUpShellProtocol().\r
3013\r
e9723321 3014 @param[in,out] NewShell The pointer to the pointer to the structure\r
a405b86d 3015 to install.\r
3016\r
3017 @retval EFI_SUCCESS The operation was successful.\r
3018 @return An error from LocateHandle, CreateEvent, or other core function.\r
3019**/\r
3020EFI_STATUS\r
3021EFIAPI\r
3022CreatePopulateInstallShellProtocol (\r
3023 IN OUT EFI_SHELL_PROTOCOL **NewShell\r
3024 )\r
3025{\r
3026 EFI_STATUS Status;\r
3027 UINTN BufferSize;\r
3028 EFI_HANDLE *Buffer;\r
3029 UINTN HandleCounter;\r
3030 SHELL_PROTOCOL_HANDLE_LIST *OldProtocolNode;\r
3031\r
8be0ba36 3032 if (NewShell == NULL) {\r
3033 return (EFI_INVALID_PARAMETER);\r
3034 }\r
3035\r
a405b86d 3036 BufferSize = 0;\r
3037 Buffer = NULL;\r
3038 OldProtocolNode = NULL;\r
3039 InitializeListHead(&ShellInfoObject.OldShellList.Link);\r
3040\r
a405b86d 3041 //\r
3042 // Initialize EfiShellProtocol object...\r
3043 //\r
a405b86d 3044 Status = gBS->CreateEvent(0,\r
3045 0,\r
3046 NULL,\r
3047 NULL,\r
3048 &mShellProtocol.ExecutionBreak);\r
8be0ba36 3049 if (EFI_ERROR(Status)) {\r
3050 return (Status);\r
3051 }\r
a405b86d 3052\r
3053 //\r
3054 // Get the size of the buffer we need.\r
3055 //\r
3056 Status = gBS->LocateHandle(ByProtocol,\r
3057 &gEfiShellProtocolGuid,\r
3058 NULL,\r
3059 &BufferSize,\r
3060 Buffer);\r
3061 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3062 //\r
3063 // Allocate and recall with buffer of correct size\r
3064 //\r
3065 Buffer = AllocateZeroPool(BufferSize);\r
8be0ba36 3066 if (Buffer == NULL) {\r
3067 return (EFI_OUT_OF_RESOURCES);\r
3068 }\r
a405b86d 3069 Status = gBS->LocateHandle(ByProtocol,\r
3070 &gEfiShellProtocolGuid,\r
3071 NULL,\r
3072 &BufferSize,\r
3073 Buffer);\r
8be0ba36 3074 if (EFI_ERROR(Status)) {\r
3075 FreePool(Buffer);\r
3076 return (Status);\r
3077 }\r
a405b86d 3078 //\r
3079 // now overwrite each of them, but save the info to restore when we end.\r
3080 //\r
3081 for (HandleCounter = 0 ; HandleCounter < (BufferSize/sizeof(EFI_HANDLE)) ; HandleCounter++) {\r
3082 OldProtocolNode = AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST));\r
3083 ASSERT(OldProtocolNode != NULL);\r
3084 Status = gBS->OpenProtocol(Buffer[HandleCounter],\r
3085 &gEfiShellProtocolGuid,\r
3086 (VOID **) &(OldProtocolNode->Interface),\r
3087 gImageHandle,\r
3088 NULL,\r
3089 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
3090 );\r
3091 if (!EFI_ERROR(Status)) {\r
3092 //\r
3093 // reinstall over the old one...\r
3094 //\r
3095 OldProtocolNode->Handle = Buffer[HandleCounter];\r
3096 Status = gBS->ReinstallProtocolInterface(\r
3097 OldProtocolNode->Handle,\r
3098 &gEfiShellProtocolGuid,\r
3099 OldProtocolNode->Interface,\r
8be0ba36 3100 (VOID*)(&mShellProtocol));\r
a405b86d 3101 if (!EFI_ERROR(Status)) {\r
3102 //\r
3103 // we reinstalled sucessfully. log this so we can reverse it later.\r
3104 //\r
3105\r
3106 //\r
3107 // add to the list for subsequent...\r
3108 //\r
3109 InsertTailList(&ShellInfoObject.OldShellList.Link, &OldProtocolNode->Link);\r
3110 }\r
3111 }\r
3112 }\r
3113 FreePool(Buffer);\r
3114 } else if (Status == EFI_NOT_FOUND) {\r
3115 ASSERT(IsListEmpty(&ShellInfoObject.OldShellList.Link));\r
3116 //\r
3117 // no one else published yet. just publish it ourselves.\r
3118 //\r
3119 Status = gBS->InstallProtocolInterface (\r
3120 &gImageHandle,\r
3121 &gEfiShellProtocolGuid,\r
3122 EFI_NATIVE_INTERFACE,\r
8be0ba36 3123 (VOID*)(&mShellProtocol));\r
a405b86d 3124 }\r
3125\r
3126 if (PcdGetBool(PcdShellSupportOldProtocols)){\r
3127 ///@todo support ShellEnvironment2\r
3128 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?\r
3129 }\r
3130\r
8be0ba36 3131 if (!EFI_ERROR(Status)) {\r
3132 *NewShell = &mShellProtocol;\r
3133 }\r
a405b86d 3134 return (Status);\r
3135}\r
3136\r
3137/**\r
e9723321 3138 Opposite of CreatePopulateInstallShellProtocol.\r
a405b86d 3139\r
3140 Free all memory and restore the system to the state it was in before calling\r
3141 CreatePopulateInstallShellProtocol.\r
3142\r
3143 @param[in,out] NewShell The pointer to the new shell protocol structure.\r
3144\r
3145 @retval EFI_SUCCESS The operation was successful.\r
3146**/\r
3147EFI_STATUS\r
3148EFIAPI\r
3149CleanUpShellProtocol (\r
3150 IN OUT EFI_SHELL_PROTOCOL *NewShell\r
3151 )\r
3152{\r
8be0ba36 3153 EFI_STATUS Status;\r
3154 SHELL_PROTOCOL_HANDLE_LIST *Node2;\r
3155 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
a405b86d 3156\r
3157 //\r
3158 // if we need to restore old protocols...\r
3159 //\r
3160 if (!IsListEmpty(&ShellInfoObject.OldShellList.Link)) {\r
3161 for (Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode(&ShellInfoObject.OldShellList.Link)\r
3162 ; !IsListEmpty (&ShellInfoObject.OldShellList.Link)\r
3163 ; Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode(&ShellInfoObject.OldShellList.Link)\r
3164 ){\r
3165 RemoveEntryList(&Node2->Link);\r
3166 Status = gBS->ReinstallProtocolInterface(Node2->Handle,\r
3167 &gEfiShellProtocolGuid,\r
3168 NewShell,\r
3169 Node2->Interface);\r
a405b86d 3170 FreePool(Node2);\r
3171 }\r
3172 } else {\r
3173 //\r
3174 // no need to restore\r
3175 //\r
3176 Status = gBS->UninstallProtocolInterface(gImageHandle,\r
3177 &gEfiShellProtocolGuid,\r
3178 NewShell);\r
a405b86d 3179 }\r
3180 Status = gBS->CloseEvent(NewShell->ExecutionBreak);\r
8be0ba36 3181 NewShell->ExecutionBreak = NULL;\r
3182\r
3183 Status = gBS->OpenProtocol(\r
3184 gST->ConsoleInHandle,\r
3185 &gEfiSimpleTextInputExProtocolGuid,\r
3186 (VOID**)&SimpleEx,\r
3187 gImageHandle,\r
3188 NULL,\r
3189 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
3190\r
3191 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle1);\r
3192 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle2);\r
a405b86d 3193\r
3194 return (Status);\r
3195}\r
3196\r
8be0ba36 3197/**\r
3198 Notification function for keystrokes.\r
3199\r
3200 @param[in] KeyData The key that was pressed.\r
3201\r
3202 @retval EFI_SUCCESS The operation was successful.\r
3203**/\r
3204EFI_STATUS\r
3205EFIAPI\r
3206NotificationFunction(\r
3207 IN EFI_KEY_DATA *KeyData\r
3208 )\r
3209{\r
3210 if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {\r
3211 return (EFI_UNSUPPORTED);\r
3212 }\r
3213 return (gBS->SignalEvent(ShellInfoObject.NewEfiShellProtocol->ExecutionBreak));\r
3214}\r
3215\r
3216/**\r
3217 Function to start monitoring for CTRL-C using SimpleTextInputEx. This \r
3218 feature's enabled state was not known when the shell initially launched.\r
3219\r
3220 @retval EFI_SUCCESS The feature is enabled.\r
3221 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.\r
3222**/\r
3223EFI_STATUS\r
3224EFIAPI\r
3225InernalEfiShellStartMonitor(\r
3226 VOID\r
3227 )\r
3228{\r
3229 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
3230 EFI_KEY_DATA KeyData;\r
3231 EFI_STATUS Status;\r
3232\r
3233 Status = gBS->OpenProtocol(\r
3234 gST->ConsoleInHandle,\r
3235 &gEfiSimpleTextInputExProtocolGuid,\r
3236 (VOID**)&SimpleEx,\r
3237 gImageHandle,\r
3238 NULL,\r
3239 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
3240 if (EFI_ERROR(Status)) {\r
3241 ShellPrintHiiEx(\r
3242 -1, \r
3243 -1, \r
3244 NULL,\r
3245 STRING_TOKEN (STR_SHELL_NO_IN_EX),\r
3246 ShellInfoObject.HiiHandle);\r
3247 return (EFI_SUCCESS);\r
3248 }\r
a405b86d 3249\r
8be0ba36 3250 if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {\r
3251 return (EFI_UNSUPPORTED);\r
3252 }\r
3253\r
3254 KeyData.KeyState.KeyToggleState = 0;\r
3255 KeyData.Key.ScanCode = 0;\r
3256 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
3257 KeyData.Key.UnicodeChar = L'c';\r
3258\r
3259 Status = SimpleEx->RegisterKeyNotify(\r
3260 SimpleEx,\r
3261 &KeyData,\r
3262 NotificationFunction,\r
3263 &ShellInfoObject.CtrlCNotifyHandle1);\r
3264 \r
3265 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
3266 if (!EFI_ERROR(Status)) {\r
3267 Status = SimpleEx->RegisterKeyNotify(\r
3268 SimpleEx,\r
3269 &KeyData,\r
3270 NotificationFunction,\r
3271 &ShellInfoObject.CtrlCNotifyHandle2);\r
3272 }\r
3273 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
3274 KeyData.Key.UnicodeChar = 3;\r
3275 if (!EFI_ERROR(Status)) {\r
3276 Status = SimpleEx->RegisterKeyNotify(\r
3277 SimpleEx,\r
3278 &KeyData,\r
3279 NotificationFunction,\r
3280 &ShellInfoObject.CtrlCNotifyHandle3);\r
3281 }\r
3282 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
3283 if (!EFI_ERROR(Status)) {\r
3284 Status = SimpleEx->RegisterKeyNotify(\r
3285 SimpleEx,\r
3286 &KeyData,\r
3287 NotificationFunction,\r
3288 &ShellInfoObject.CtrlCNotifyHandle4);\r
3289 }\r
3290 return (Status);\r
3291}\r