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