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