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