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