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