]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
ShellPkg: Refactor string manipulation in UefiShellLib command
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
CommitLineData
94b17fa1 1/** @file\r
2 Provides interface to shell functionality for shell commands and applications.\r
3\r
75a5e2ef 4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
1e6e84c7 5 This program and the accompanying materials\r
b3011f40 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
94b17fa1 9\r
b3011f40 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
94b17fa1 12\r
13**/\r
14\r
b1f95a06 15#include "UefiShellLib.h"\r
a405b86d 16#include <ShellBase.h>\r
252d9457 17#include <Library/SortLib.h>\r
d2b4564b 18\r
94b17fa1 19#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
20\r
d2b4564b 21//\r
a405b86d 22// globals...\r
d2b4564b 23//\r
24SHELL_PARAM_ITEM EmptyParamList[] = {\r
25 {NULL, TypeMax}\r
26 };\r
a405b86d 27SHELL_PARAM_ITEM SfoParamList[] = {\r
28 {L"-sfo", TypeFlag},\r
29 {NULL, TypeMax}\r
30 };\r
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
32EFI_SHELL_INTERFACE *mEfiShellInterface;\r
366f81a0 33EFI_SHELL_PROTOCOL *gEfiShellProtocol;\r
34EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;\r
a405b86d 35EFI_HANDLE mEfiShellEnvironment2Handle;\r
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
b3011f40 37\r
2247dde4 38/**\r
39 Check if a Unicode character is a hexadecimal character.\r
40\r
1e6e84c7 41 This internal function checks if a Unicode character is a\r
a405b86d 42 numeric character. The valid hexadecimal characters are\r
2247dde4 43 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
44\r
2247dde4 45 @param Char The character to check against.\r
46\r
47 @retval TRUE If the Char is a hexadecmial character.\r
48 @retval FALSE If the Char is not a hexadecmial character.\r
49\r
50**/\r
51BOOLEAN\r
52EFIAPI\r
969c783b 53ShellIsHexaDecimalDigitCharacter (\r
2247dde4 54 IN CHAR16 Char\r
a405b86d 55 )\r
56{\r
2247dde4 57 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
58}\r
94b17fa1 59\r
60/**\r
a405b86d 61 Check if a Unicode character is a decimal character.\r
62\r
63 This internal function checks if a Unicode character is a\r
64 decimal character. The valid characters are\r
65 L'0' to L'9'.\r
66\r
67\r
68 @param Char The character to check against.\r
69\r
70 @retval TRUE If the Char is a hexadecmial character.\r
71 @retval FALSE If the Char is not a hexadecmial character.\r
72\r
73**/\r
74BOOLEAN\r
75EFIAPI\r
76ShellIsDecimalDigitCharacter (\r
77 IN CHAR16 Char\r
78 )\r
79{\r
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r
81}\r
82\r
83/**\r
84 Helper function to find ShellEnvironment2 for constructor.\r
85\r
86 @param[in] ImageHandle A copy of the calling image's handle.\r
beab0fc5 87\r
88 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
94b17fa1 89**/\r
90EFI_STATUS\r
91EFIAPI\r
92ShellFindSE2 (\r
93 IN EFI_HANDLE ImageHandle\r
a405b86d 94 )\r
95{\r
94b17fa1 96 EFI_STATUS Status;\r
97 EFI_HANDLE *Buffer;\r
98 UINTN BufferSize;\r
99 UINTN HandleIndex;\r
100\r
101 BufferSize = 0;\r
102 Buffer = NULL;\r
1e6e84c7 103 Status = gBS->OpenProtocol(ImageHandle,\r
94b17fa1 104 &gEfiShellEnvironment2Guid,\r
105 (VOID **)&mEfiShellEnvironment2,\r
106 ImageHandle,\r
107 NULL,\r
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
a405b86d 109 );\r
94b17fa1 110 //\r
111 // look for the mEfiShellEnvironment2 protocol at a higher level\r
112 //\r
a405b86d 113 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){\r
94b17fa1 114 //\r
115 // figure out how big of a buffer we need.\r
116 //\r
117 Status = gBS->LocateHandle (ByProtocol,\r
118 &gEfiShellEnvironment2Guid,\r
119 NULL, // ignored for ByProtocol\r
120 &BufferSize,\r
121 Buffer\r
a405b86d 122 );\r
2247dde4 123 //\r
124 // maybe it's not there???\r
125 //\r
126 if (Status == EFI_BUFFER_TOO_SMALL) {\r
252d9457 127 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);\r
beab0fc5 128 if (Buffer == NULL) {\r
129 return (EFI_OUT_OF_RESOURCES);\r
130 }\r
2247dde4 131 Status = gBS->LocateHandle (ByProtocol,\r
132 &gEfiShellEnvironment2Guid,\r
133 NULL, // ignored for ByProtocol\r
134 &BufferSize,\r
135 Buffer\r
a405b86d 136 );\r
2247dde4 137 }\r
1cd45e78 138 if (!EFI_ERROR (Status) && Buffer != NULL) {\r
94b17fa1 139 //\r
140 // now parse the list of returned handles\r
141 //\r
142 Status = EFI_NOT_FOUND;\r
143 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r
1e6e84c7 144 Status = gBS->OpenProtocol(Buffer[HandleIndex],\r
94b17fa1 145 &gEfiShellEnvironment2Guid,\r
146 (VOID **)&mEfiShellEnvironment2,\r
147 ImageHandle,\r
148 NULL,\r
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
a405b86d 150 );\r
151 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {\r
94b17fa1 152 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
153 Status = EFI_SUCCESS;\r
154 break;\r
155 }\r
156 }\r
157 }\r
158 }\r
159 if (Buffer != NULL) {\r
160 FreePool (Buffer);\r
161 }\r
162 return (Status);\r
163}\r
164\r
252d9457 165/**\r
b0934ac4 166 Function to do most of the work of the constructor. Allows for calling\r
a405b86d 167 multiple times without complete re-initialization.\r
168\r
169 @param[in] ImageHandle A copy of the ImageHandle.\r
170 @param[in] SystemTable A pointer to the SystemTable for the application.\r
252d9457 171\r
172 @retval EFI_SUCCESS The operationw as successful.\r
a405b86d 173**/\r
94b17fa1 174EFI_STATUS\r
175EFIAPI\r
d2b4564b 176ShellLibConstructorWorker (\r
94b17fa1 177 IN EFI_HANDLE ImageHandle,\r
178 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 179 )\r
180{\r
181 EFI_STATUS Status;\r
ecd3d59f 182\r
94b17fa1 183 //\r
184 // UEFI 2.0 shell interfaces (used preferentially)\r
185 //\r
a405b86d 186 Status = gBS->OpenProtocol(\r
187 ImageHandle,\r
188 &gEfiShellProtocolGuid,\r
366f81a0 189 (VOID **)&gEfiShellProtocol,\r
a405b86d 190 ImageHandle,\r
191 NULL,\r
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
193 );\r
94b17fa1 194 if (EFI_ERROR(Status)) {\r
a405b86d 195 //\r
196 // Search for the shell protocol\r
197 //\r
198 Status = gBS->LocateProtocol(\r
199 &gEfiShellProtocolGuid,\r
200 NULL,\r
366f81a0 201 (VOID **)&gEfiShellProtocol\r
a405b86d 202 );\r
203 if (EFI_ERROR(Status)) {\r
366f81a0 204 gEfiShellProtocol = NULL;\r
a405b86d 205 }\r
94b17fa1 206 }\r
a405b86d 207 Status = gBS->OpenProtocol(\r
208 ImageHandle,\r
209 &gEfiShellParametersProtocolGuid,\r
366f81a0 210 (VOID **)&gEfiShellParametersProtocol,\r
a405b86d 211 ImageHandle,\r
212 NULL,\r
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
214 );\r
94b17fa1 215 if (EFI_ERROR(Status)) {\r
366f81a0 216 gEfiShellParametersProtocol = NULL;\r
94b17fa1 217 }\r
218\r
366f81a0 219 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {\r
94b17fa1 220 //\r
221 // Moved to seperate function due to complexity\r
222 //\r
223 Status = ShellFindSE2(ImageHandle);\r
224\r
225 if (EFI_ERROR(Status)) {\r
226 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
227 mEfiShellEnvironment2 = NULL;\r
228 }\r
1e6e84c7 229 Status = gBS->OpenProtocol(ImageHandle,\r
94b17fa1 230 &gEfiShellInterfaceGuid,\r
231 (VOID **)&mEfiShellInterface,\r
232 ImageHandle,\r
233 NULL,\r
234 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
a405b86d 235 );\r
94b17fa1 236 if (EFI_ERROR(Status)) {\r
237 mEfiShellInterface = NULL;\r
238 }\r
239 }\r
c9d92df0 240\r
94b17fa1 241 //\r
242 // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
243 //\r
1e6e84c7 244 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r
366f81a0 245 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {\r
246 if (gEfiShellProtocol != NULL) {\r
247 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;\r
248 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;\r
249 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;\r
250 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;\r
251 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;\r
252 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;\r
253 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;\r
254 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;\r
255 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;\r
256 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;\r
d2b4564b 257 } else {\r
a405b86d 258 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;\r
259 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;\r
260 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;\r
261 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;\r
262 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;\r
263 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;\r
264 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;\r
265 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;\r
266 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;\r
267 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;\r
d2b4564b 268 }\r
94b17fa1 269 return (EFI_SUCCESS);\r
270 }\r
271 return (EFI_NOT_FOUND);\r
272}\r
d2b4564b 273/**\r
274 Constructor for the Shell library.\r
275\r
276 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
277\r
278 @param ImageHandle the image handle of the process\r
279 @param SystemTable the EFI System Table pointer\r
280\r
281 @retval EFI_SUCCESS the initialization was complete sucessfully\r
282 @return others an error ocurred during initialization\r
283**/\r
284EFI_STATUS\r
285EFIAPI\r
286ShellLibConstructor (\r
287 IN EFI_HANDLE ImageHandle,\r
288 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 289 )\r
290{\r
d2b4564b 291 mEfiShellEnvironment2 = NULL;\r
366f81a0 292 gEfiShellProtocol = NULL;\r
293 gEfiShellParametersProtocol = NULL;\r
d2b4564b 294 mEfiShellInterface = NULL;\r
295 mEfiShellEnvironment2Handle = NULL;\r
296\r
d2b4564b 297 //\r
298 // verify that auto initialize is not set false\r
1e6e84c7 299 //\r
d2b4564b 300 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r
301 return (EFI_SUCCESS);\r
302 }\r
1e6e84c7 303\r
d2b4564b 304 return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r
305}\r
94b17fa1 306\r
307/**\r
a405b86d 308 Destructor for the library. free any resources.\r
309\r
310 @param[in] ImageHandle A copy of the ImageHandle.\r
311 @param[in] SystemTable A pointer to the SystemTable for the application.\r
312\r
313 @retval EFI_SUCCESS The operation was successful.\r
314 @return An error from the CloseProtocol function.\r
94b17fa1 315**/\r
316EFI_STATUS\r
317EFIAPI\r
318ShellLibDestructor (\r
319 IN EFI_HANDLE ImageHandle,\r
320 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 321 )\r
322{\r
94b17fa1 323 if (mEfiShellEnvironment2 != NULL) {\r
324 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
325 &gEfiShellEnvironment2Guid,\r
326 ImageHandle,\r
327 NULL);\r
d2b4564b 328 mEfiShellEnvironment2 = NULL;\r
94b17fa1 329 }\r
330 if (mEfiShellInterface != NULL) {\r
331 gBS->CloseProtocol(ImageHandle,\r
332 &gEfiShellInterfaceGuid,\r
333 ImageHandle,\r
1e6e84c7 334 NULL);\r
d2b4564b 335 mEfiShellInterface = NULL;\r
94b17fa1 336 }\r
366f81a0 337 if (gEfiShellProtocol != NULL) {\r
94b17fa1 338 gBS->CloseProtocol(ImageHandle,\r
339 &gEfiShellProtocolGuid,\r
340 ImageHandle,\r
1e6e84c7 341 NULL);\r
366f81a0 342 gEfiShellProtocol = NULL;\r
94b17fa1 343 }\r
366f81a0 344 if (gEfiShellParametersProtocol != NULL) {\r
94b17fa1 345 gBS->CloseProtocol(ImageHandle,\r
346 &gEfiShellParametersProtocolGuid,\r
347 ImageHandle,\r
1e6e84c7 348 NULL);\r
366f81a0 349 gEfiShellParametersProtocol = NULL;\r
94b17fa1 350 }\r
d2b4564b 351 mEfiShellEnvironment2Handle = NULL;\r
ecd3d59f 352\r
94b17fa1 353 return (EFI_SUCCESS);\r
354}\r
d2b4564b 355\r
356/**\r
357 This function causes the shell library to initialize itself. If the shell library\r
358 is already initialized it will de-initialize all the current protocol poitners and\r
359 re-populate them again.\r
360\r
361 When the library is used with PcdShellLibAutoInitialize set to true this function\r
362 will return EFI_SUCCESS and perform no actions.\r
363\r
364 This function is intended for internal access for shell commands only.\r
365\r
366 @retval EFI_SUCCESS the initialization was complete sucessfully\r
367\r
368**/\r
369EFI_STATUS\r
370EFIAPI\r
371ShellInitialize (\r
a405b86d 372 )\r
373{\r
d2b4564b 374 //\r
375 // if auto initialize is not false then skip\r
376 //\r
377 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {\r
378 return (EFI_SUCCESS);\r
379 }\r
380\r
381 //\r
382 // deinit the current stuff\r
383 //\r
384 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r
385\r
386 //\r
387 // init the new stuff\r
388 //\r
389 return (ShellLibConstructorWorker(gImageHandle, gST));\r
390}\r
391\r
94b17fa1 392/**\r
1e6e84c7 393 This function will retrieve the information about the file for the handle\r
94b17fa1 394 specified and store it in allocated pool memory.\r
395\r
1e6e84c7 396 This function allocates a buffer to store the file's information. It is the\r
69817bf8 397 caller's responsibility to free the buffer\r
94b17fa1 398\r
1e6e84c7 399 @param FileHandle The file handle of the file for which information is\r
94b17fa1 400 being requested.\r
401\r
402 @retval NULL information could not be retrieved.\r
403\r
404 @return the information about the file\r
405**/\r
406EFI_FILE_INFO*\r
407EFIAPI\r
408ShellGetFileInfo (\r
a405b86d 409 IN SHELL_FILE_HANDLE FileHandle\r
410 )\r
411{\r
d2b4564b 412 return (FileFunctionMap.GetFileInfo(FileHandle));\r
94b17fa1 413}\r
414\r
415/**\r
a405b86d 416 This function sets the information about the file for the opened handle\r
94b17fa1 417 specified.\r
418\r
a405b86d 419 @param[in] FileHandle The file handle of the file for which information\r
420 is being set.\r
94b17fa1 421\r
a405b86d 422 @param[in] FileInfo The information to set.\r
94b17fa1 423\r
b0934ac4 424 @retval EFI_SUCCESS The information was set.\r
a405b86d 425 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.\r
426 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.\r
b0934ac4 427 @retval EFI_NO_MEDIA The device has no medium.\r
428 @retval EFI_DEVICE_ERROR The device reported an error.\r
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
a405b86d 431 @retval EFI_ACCESS_DENIED The file was opened read only.\r
432 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 433**/\r
434EFI_STATUS\r
435EFIAPI\r
436ShellSetFileInfo (\r
b0934ac4 437 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 438 IN EFI_FILE_INFO *FileInfo\r
a405b86d 439 )\r
440{\r
d2b4564b 441 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r
1e6e84c7 442}\r
443\r
94b17fa1 444 /**\r
445 This function will open a file or directory referenced by DevicePath.\r
446\r
1e6e84c7 447 This function opens a file with the open mode according to the file path. The\r
94b17fa1 448 Attributes is valid only for EFI_FILE_MODE_CREATE.\r
449\r
b0934ac4 450 @param FilePath on input the device path to the file. On output\r
94b17fa1 451 the remaining device path.\r
b0934ac4 452 @param DeviceHandle pointer to the system device handle.\r
453 @param FileHandle pointer to the file handle.\r
454 @param OpenMode the mode to open the file with.\r
455 @param Attributes the file's file attributes.\r
456\r
457 @retval EFI_SUCCESS The information was set.\r
458 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
459 @retval EFI_UNSUPPORTED Could not open the file path.\r
460 @retval EFI_NOT_FOUND The specified file could not be found on the\r
1e6e84c7 461 device or the file system could not be found on\r
94b17fa1 462 the device.\r
b0934ac4 463 @retval EFI_NO_MEDIA The device has no medium.\r
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 465 medium is no longer supported.\r
b0934ac4 466 @retval EFI_DEVICE_ERROR The device reported an error.\r
467 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
468 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
469 @retval EFI_ACCESS_DENIED The file was opened read only.\r
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 471 file.\r
b0934ac4 472 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 473**/\r
474EFI_STATUS\r
475EFIAPI\r
476ShellOpenFileByDevicePath(\r
b0934ac4 477 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
478 OUT EFI_HANDLE *DeviceHandle,\r
a405b86d 479 OUT SHELL_FILE_HANDLE *FileHandle,\r
b0934ac4 480 IN UINT64 OpenMode,\r
481 IN UINT64 Attributes\r
a405b86d 482 )\r
483{\r
484 CHAR16 *FileName;\r
485 EFI_STATUS Status;\r
94b17fa1 486 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
a405b86d 487 EFI_FILE_PROTOCOL *Handle1;\r
488 EFI_FILE_PROTOCOL *Handle2;\r
0b6cb335
ED
489 CHAR16 *FnafPathName;\r
490 UINTN PathLen;\r
94b17fa1 491\r
92a5447e 492 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {\r
493 return (EFI_INVALID_PARAMETER);\r
494 }\r
495\r
1e6e84c7 496 //\r
94b17fa1 497 // which shell interface should we use\r
498 //\r
366f81a0 499 if (gEfiShellProtocol != NULL) {\r
94b17fa1 500 //\r
501 // use UEFI Shell 2.0 method.\r
502 //\r
366f81a0 503 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
94b17fa1 504 if (FileName == NULL) {\r
505 return (EFI_INVALID_PARAMETER);\r
506 }\r
507 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);\r
508 FreePool(FileName);\r
509 return (Status);\r
1e6e84c7 510 }\r
d2b4564b 511\r
512\r
513 //\r
514 // use old shell method.\r
515 //\r
1e6e84c7 516 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,\r
517 FilePath,\r
d2b4564b 518 DeviceHandle);\r
519 if (EFI_ERROR (Status)) {\r
520 return Status;\r
521 }\r
522 Status = gBS->OpenProtocol(*DeviceHandle,\r
523 &gEfiSimpleFileSystemProtocolGuid,\r
b1f95a06 524 (VOID**)&EfiSimpleFileSystemProtocol,\r
d2b4564b 525 gImageHandle,\r
526 NULL,\r
527 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
528 if (EFI_ERROR (Status)) {\r
529 return Status;\r
530 }\r
a405b86d 531 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
d2b4564b 532 if (EFI_ERROR (Status)) {\r
533 FileHandle = NULL;\r
534 return Status;\r
535 }\r
536\r
537 //\r
538 // go down directories one node at a time.\r
539 //\r
540 while (!IsDevicePathEnd (*FilePath)) {\r
94b17fa1 541 //\r
d2b4564b 542 // For file system access each node should be a file path component\r
94b17fa1 543 //\r
d2b4564b 544 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
545 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
a405b86d 546 ) {\r
94b17fa1 547 FileHandle = NULL;\r
d2b4564b 548 return (EFI_INVALID_PARAMETER);\r
94b17fa1 549 }\r
d2b4564b 550 //\r
551 // Open this file path node\r
552 //\r
a405b86d 553 Handle2 = Handle1;\r
554 Handle1 = NULL;\r
94b17fa1 555\r
0b6cb335
ED
556 //\r
557 // File Name Alignment Fix (FNAF)\r
558 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.\r
559 // The structure pointed to by FilePath may be not CHAR16 aligned.\r
560 // This code copies the potentially unaligned PathName data from the\r
561 // FilePath structure to the aligned FnafPathName for use in the\r
562 // calls to Handl2->Open.\r
563 //\r
564\r
565 //\r
566 // Determine length of PathName, in bytes.\r
567 //\r
568 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;\r
569\r
570 //\r
571 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment\r
572 // Copy bytes from possibly unaligned location to aligned location\r
573 //\r
574 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);\r
575 if (FnafPathName == NULL) {\r
576 return EFI_OUT_OF_RESOURCES;\r
577 }\r
578\r
94b17fa1 579 //\r
d2b4564b 580 // Try to test opening an existing file\r
94b17fa1 581 //\r
a405b86d 582 Status = Handle2->Open (\r
583 Handle2,\r
584 &Handle1,\r
0b6cb335 585 FnafPathName,\r
d2b4564b 586 OpenMode &~EFI_FILE_MODE_CREATE,\r
587 0\r
a405b86d 588 );\r
94b17fa1 589\r
d2b4564b 590 //\r
591 // see if the error was that it needs to be created\r
592 //\r
593 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
a405b86d 594 Status = Handle2->Open (\r
595 Handle2,\r
596 &Handle1,\r
0b6cb335 597 FnafPathName,\r
d2b4564b 598 OpenMode,\r
599 Attributes\r
a405b86d 600 );\r
d2b4564b 601 }\r
0b6cb335
ED
602\r
603 //\r
604 // Free the alignment buffer\r
605 //\r
606 FreePool(FnafPathName);\r
607\r
d2b4564b 608 //\r
609 // Close the last node\r
610 //\r
a405b86d 611 Handle2->Close (Handle2);\r
94b17fa1 612\r
d2b4564b 613 if (EFI_ERROR(Status)) {\r
614 return (Status);\r
94b17fa1 615 }\r
d2b4564b 616\r
617 //\r
618 // Get the next node\r
619 //\r
620 *FilePath = NextDevicePathNode (*FilePath);\r
94b17fa1 621 }\r
a405b86d 622\r
623 //\r
624 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
625 //\r
626 *FileHandle = (VOID*)Handle1;\r
d2b4564b 627 return (EFI_SUCCESS);\r
94b17fa1 628}\r
629\r
630/**\r
631 This function will open a file or directory referenced by filename.\r
632\r
1e6e84c7 633 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;\r
634 otherwise, the Filehandle is NULL. The Attributes is valid only for\r
94b17fa1 635 EFI_FILE_MODE_CREATE.\r
636\r
92a5447e 637 if FileName is NULL then ASSERT()\r
94b17fa1 638\r
b0934ac4 639 @param FileName pointer to file name\r
640 @param FileHandle pointer to the file handle.\r
641 @param OpenMode the mode to open the file with.\r
642 @param Attributes the file's file attributes.\r
94b17fa1 643\r
b0934ac4 644 @retval EFI_SUCCESS The information was set.\r
645 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
646 @retval EFI_UNSUPPORTED Could not open the file path.\r
647 @retval EFI_NOT_FOUND The specified file could not be found on the\r
1e6e84c7 648 device or the file system could not be found\r
94b17fa1 649 on the device.\r
b0934ac4 650 @retval EFI_NO_MEDIA The device has no medium.\r
651 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 652 medium is no longer supported.\r
b0934ac4 653 @retval EFI_DEVICE_ERROR The device reported an error.\r
654 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
655 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
656 @retval EFI_ACCESS_DENIED The file was opened read only.\r
657 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 658 file.\r
b0934ac4 659 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 660**/\r
661EFI_STATUS\r
662EFIAPI\r
663ShellOpenFileByName(\r
b0934ac4 664 IN CONST CHAR16 *FileName,\r
a405b86d 665 OUT SHELL_FILE_HANDLE *FileHandle,\r
94b17fa1 666 IN UINT64 OpenMode,\r
b0934ac4 667 IN UINT64 Attributes\r
a405b86d 668 )\r
669{\r
94b17fa1 670 EFI_HANDLE DeviceHandle;\r
671 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
b1f95a06 672 EFI_STATUS Status;\r
673 EFI_FILE_INFO *FileInfo;\r
94b17fa1 674\r
675 //\r
676 // ASSERT if FileName is NULL\r
677 //\r
678 ASSERT(FileName != NULL);\r
679\r
a405b86d 680 if (FileName == NULL) {\r
681 return (EFI_INVALID_PARAMETER);\r
682 }\r
683\r
366f81a0 684 if (gEfiShellProtocol != NULL) {\r
a405b86d 685 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
686 return ShellCreateDirectory(FileName, FileHandle);\r
687 }\r
94b17fa1 688 //\r
689 // Use UEFI Shell 2.0 method\r
690 //\r
366f81a0 691 Status = gEfiShellProtocol->OpenFileByName(FileName,\r
b1f95a06 692 FileHandle,\r
693 OpenMode);\r
a405b86d 694 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
2247dde4 695 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
b1f95a06 696 ASSERT(FileInfo != NULL);\r
697 FileInfo->Attribute = Attributes;\r
2247dde4 698 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r
699 FreePool(FileInfo);\r
b1f95a06 700 }\r
701 return (Status);\r
1e6e84c7 702 }\r
94b17fa1 703 //\r
704 // Using EFI Shell version\r
705 // this means convert name to path and call that function\r
706 // since this will use EFI method again that will open it.\r
707 //\r
708 ASSERT(mEfiShellEnvironment2 != NULL);\r
b82bfcc1 709 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
90bfa227 710 if (FilePath != NULL) {\r
94b17fa1 711 return (ShellOpenFileByDevicePath(&FilePath,\r
712 &DeviceHandle,\r
713 FileHandle,\r
714 OpenMode,\r
a405b86d 715 Attributes));\r
94b17fa1 716 }\r
717 return (EFI_DEVICE_ERROR);\r
718}\r
719/**\r
720 This function create a directory\r
721\r
1e6e84c7 722 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;\r
723 otherwise, the Filehandle is NULL. If the directory already existed, this\r
94b17fa1 724 function opens the existing directory.\r
725\r
b0934ac4 726 @param DirectoryName pointer to directory name\r
727 @param FileHandle pointer to the file handle.\r
94b17fa1 728\r
b0934ac4 729 @retval EFI_SUCCESS The information was set.\r
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
731 @retval EFI_UNSUPPORTED Could not open the file path.\r
732 @retval EFI_NOT_FOUND The specified file could not be found on the\r
1e6e84c7 733 device or the file system could not be found\r
94b17fa1 734 on the device.\r
b0934ac4 735 @retval EFI_NO_MEDIA The device has no medium.\r
736 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 737 medium is no longer supported.\r
b0934ac4 738 @retval EFI_DEVICE_ERROR The device reported an error.\r
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
740 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
741 @retval EFI_ACCESS_DENIED The file was opened read only.\r
742 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 743 file.\r
b0934ac4 744 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 745 @sa ShellOpenFileByName\r
746**/\r
747EFI_STATUS\r
748EFIAPI\r
749ShellCreateDirectory(\r
b82bfcc1 750 IN CONST CHAR16 *DirectoryName,\r
a405b86d 751 OUT SHELL_FILE_HANDLE *FileHandle\r
752 )\r
753{\r
366f81a0 754 if (gEfiShellProtocol != NULL) {\r
2247dde4 755 //\r
756 // Use UEFI Shell 2.0 method\r
757 //\r
366f81a0 758 return (gEfiShellProtocol->CreateFile(DirectoryName,\r
2247dde4 759 EFI_FILE_DIRECTORY,\r
760 FileHandle\r
a405b86d 761 ));\r
2247dde4 762 } else {\r
763 return (ShellOpenFileByName(DirectoryName,\r
764 FileHandle,\r
765 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r
766 EFI_FILE_DIRECTORY\r
a405b86d 767 ));\r
2247dde4 768 }\r
94b17fa1 769}\r
770\r
771/**\r
772 This function reads information from an opened file.\r
773\r
1e6e84c7 774 If FileHandle is not a directory, the function reads the requested number of\r
775 bytes from the file at the file's current position and returns them in Buffer.\r
94b17fa1 776 If the read goes beyond the end of the file, the read length is truncated to the\r
1e6e84c7 777 end of the file. The file's current position is increased by the number of bytes\r
778 returned. If FileHandle is a directory, the function reads the directory entry\r
779 at the file's current position and returns the entry in Buffer. If the Buffer\r
780 is not large enough to hold the current directory entry, then\r
781 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.\r
782 BufferSize is set to be the size of the buffer needed to read the entry. On\r
783 success, the current position is updated to the next directory entry. If there\r
784 are no more directory entries, the read returns a zero-length buffer.\r
94b17fa1 785 EFI_FILE_INFO is the structure returned as the directory entry.\r
786\r
787 @param FileHandle the opened file handle\r
1e6e84c7 788 @param BufferSize on input the size of buffer in bytes. on return\r
94b17fa1 789 the number of bytes written.\r
790 @param Buffer the buffer to put read data into.\r
791\r
b0934ac4 792 @retval EFI_SUCCESS Data was read.\r
793 @retval EFI_NO_MEDIA The device has no media.\r
794 @retval EFI_DEVICE_ERROR The device reported an error.\r
795 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
796 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required\r
94b17fa1 797 size.\r
798\r
799**/\r
800EFI_STATUS\r
801EFIAPI\r
802ShellReadFile(\r
a405b86d 803 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 804 IN OUT UINTN *BufferSize,\r
805 OUT VOID *Buffer\r
a405b86d 806 )\r
807{\r
d2b4564b 808 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 809}\r
810\r
811\r
812/**\r
813 Write data to a file.\r
814\r
1e6e84c7 815 This function writes the specified number of bytes to the file at the current\r
816 file position. The current file position is advanced the actual number of bytes\r
817 written, which is returned in BufferSize. Partial writes only occur when there\r
818 has been a data error during the write attempt (such as "volume space full").\r
819 The file is automatically grown to hold the data if required. Direct writes to\r
94b17fa1 820 opened directories are not supported.\r
821\r
822 @param FileHandle The opened file for writing\r
823 @param BufferSize on input the number of bytes in Buffer. On output\r
824 the number of bytes written.\r
825 @param Buffer the buffer containing data to write is stored.\r
826\r
b0934ac4 827 @retval EFI_SUCCESS Data was written.\r
828 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
829 @retval EFI_NO_MEDIA The device has no media.\r
830 @retval EFI_DEVICE_ERROR The device reported an error.\r
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
832 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
833 @retval EFI_ACCESS_DENIED The file was open for read only.\r
834 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 835**/\r
836EFI_STATUS\r
837EFIAPI\r
838ShellWriteFile(\r
252d9457 839 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 840 IN OUT UINTN *BufferSize,\r
841 IN VOID *Buffer\r
a405b86d 842 )\r
843{\r
d2b4564b 844 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 845}\r
846\r
1e6e84c7 847/**\r
94b17fa1 848 Close an open file handle.\r
849\r
1e6e84c7 850 This function closes a specified file handle. All "dirty" cached file data is\r
851 flushed to the device, and the file is closed. In all cases the handle is\r
94b17fa1 852 closed.\r
853\r
854@param FileHandle the file handle to close.\r
855\r
856@retval EFI_SUCCESS the file handle was closed sucessfully.\r
857**/\r
858EFI_STATUS\r
859EFIAPI\r
860ShellCloseFile (\r
a405b86d 861 IN SHELL_FILE_HANDLE *FileHandle\r
862 )\r
863{\r
d2b4564b 864 return (FileFunctionMap.CloseFile(*FileHandle));\r
94b17fa1 865}\r
866\r
867/**\r
868 Delete a file and close the handle\r
869\r
870 This function closes and deletes a file. In all cases the file handle is closed.\r
1e6e84c7 871 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is\r
94b17fa1 872 returned, but the handle is still closed.\r
873\r
874 @param FileHandle the file handle to delete\r
875\r
876 @retval EFI_SUCCESS the file was closed sucessfully\r
1e6e84c7 877 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
94b17fa1 878 deleted\r
b0934ac4 879 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
94b17fa1 880**/\r
881EFI_STATUS\r
882EFIAPI\r
883ShellDeleteFile (\r
b0934ac4 884 IN SHELL_FILE_HANDLE *FileHandle\r
a405b86d 885 )\r
886{\r
d2b4564b 887 return (FileFunctionMap.DeleteFile(*FileHandle));\r
94b17fa1 888}\r
889\r
890/**\r
891 Set the current position in a file.\r
892\r
1e6e84c7 893 This function sets the current file position for the handle to the position\r
94b17fa1 894 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
1e6e84c7 895 absolute positioning is supported, and seeking past the end of the file is\r
896 allowed (a subsequent write would grow the file). Seeking to position\r
94b17fa1 897 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
1e6e84c7 898 If FileHandle is a directory, the only position that may be set is zero. This\r
94b17fa1 899 has the effect of starting the read process of the directory entries over.\r
900\r
901 @param FileHandle The file handle on which the position is being set\r
902 @param Position Byte position from begining of file\r
903\r
904 @retval EFI_SUCCESS Operation completed sucessfully.\r
1e6e84c7 905 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on\r
94b17fa1 906 directories.\r
907 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
908**/\r
909EFI_STATUS\r
910EFIAPI\r
911ShellSetFilePosition (\r
b0934ac4 912 IN SHELL_FILE_HANDLE FileHandle,\r
913 IN UINT64 Position\r
a405b86d 914 )\r
915{\r
d2b4564b 916 return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r
94b17fa1 917}\r
918\r
1e6e84c7 919/**\r
94b17fa1 920 Gets a file's current position\r
921\r
1e6e84c7 922 This function retrieves the current file position for the file handle. For\r
923 directories, the current file position has no meaning outside of the file\r
94b17fa1 924 system driver and as such the operation is not supported. An error is returned\r
925 if FileHandle is a directory.\r
926\r
927 @param FileHandle The open file handle on which to get the position.\r
928 @param Position Byte position from begining of file.\r
929\r
930 @retval EFI_SUCCESS the operation completed sucessfully.\r
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
932 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
933**/\r
934EFI_STATUS\r
935EFIAPI\r
936ShellGetFilePosition (\r
a405b86d 937 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 938 OUT UINT64 *Position\r
a405b86d 939 )\r
940{\r
d2b4564b 941 return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r
94b17fa1 942}\r
943/**\r
944 Flushes data on a file\r
1e6e84c7 945\r
94b17fa1 946 This function flushes all modified data associated with a file to a device.\r
947\r
948 @param FileHandle The file handle on which to flush data\r
949\r
950 @retval EFI_SUCCESS The data was flushed.\r
951 @retval EFI_NO_MEDIA The device has no media.\r
952 @retval EFI_DEVICE_ERROR The device reported an error.\r
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
954 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
955 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
956**/\r
957EFI_STATUS\r
958EFIAPI\r
959ShellFlushFile (\r
a405b86d 960 IN SHELL_FILE_HANDLE FileHandle\r
961 )\r
962{\r
d2b4564b 963 return (FileFunctionMap.FlushFile(FileHandle));\r
94b17fa1 964}\r
965\r
b0934ac4 966/** Retrieve first entry from a directory.\r
94b17fa1 967\r
b0934ac4 968 This function takes an open directory handle and gets information from the\r
969 first entry in the directory. A buffer is allocated to contain\r
970 the information and a pointer to the buffer is returned in *Buffer. The\r
971 caller can use ShellFindNextFile() to get subsequent directory entries.\r
94b17fa1 972\r
b0934ac4 973 The buffer will be freed by ShellFindNextFile() when the last directory\r
974 entry is read. Otherwise, the caller must free the buffer, using FreePool,\r
975 when finished with it.\r
976\r
977 @param[in] DirHandle The file handle of the directory to search.\r
978 @param[out] Buffer The pointer to the buffer for the file's information.\r
94b17fa1 979\r
980 @retval EFI_SUCCESS Found the first file.\r
981 @retval EFI_NOT_FOUND Cannot find the directory.\r
982 @retval EFI_NO_MEDIA The device has no media.\r
983 @retval EFI_DEVICE_ERROR The device reported an error.\r
984 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
985 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
986 or ShellReadFile\r
987**/\r
988EFI_STATUS\r
989EFIAPI\r
990ShellFindFirstFile (\r
a405b86d 991 IN SHELL_FILE_HANDLE DirHandle,\r
d2b4564b 992 OUT EFI_FILE_INFO **Buffer\r
a405b86d 993 )\r
994{\r
94b17fa1 995 //\r
d2b4564b 996 // pass to file handle lib\r
94b17fa1 997 //\r
d2b4564b 998 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
94b17fa1 999}\r
b0934ac4 1000/** Retrieve next entries from a directory.\r
94b17fa1 1001\r
b0934ac4 1002 To use this function, the caller must first call the ShellFindFirstFile()\r
1003 function to get the first directory entry. Subsequent directory entries are\r
1004 retrieved by using the ShellFindNextFile() function. This function can\r
1005 be called several times to get each entry from the directory. If the call of\r
1006 ShellFindNextFile() retrieved the last directory entry, the next call of\r
1007 this function will set *NoFile to TRUE and free the buffer.\r
94b17fa1 1008\r
b0934ac4 1009 @param[in] DirHandle The file handle of the directory.\r
1010 @param[out] Buffer The pointer to buffer for file's information.\r
1011 @param[out] NoFile The pointer to boolean when last file is found.\r
94b17fa1 1012\r
1013 @retval EFI_SUCCESS Found the next file, or reached last file\r
1014 @retval EFI_NO_MEDIA The device has no media.\r
1015 @retval EFI_DEVICE_ERROR The device reported an error.\r
1016 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1017**/\r
1018EFI_STATUS\r
1019EFIAPI\r
1020ShellFindNextFile(\r
a405b86d 1021 IN SHELL_FILE_HANDLE DirHandle,\r
94b17fa1 1022 OUT EFI_FILE_INFO *Buffer,\r
1023 OUT BOOLEAN *NoFile\r
a405b86d 1024 )\r
1025{\r
94b17fa1 1026 //\r
d2b4564b 1027 // pass to file handle lib\r
94b17fa1 1028 //\r
d2b4564b 1029 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
94b17fa1 1030}\r
1031/**\r
1032 Retrieve the size of a file.\r
1033\r
1034 if FileHandle is NULL then ASSERT()\r
1035 if Size is NULL then ASSERT()\r
1036\r
1e6e84c7 1037 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
94b17fa1 1038 data.\r
1039\r
1040 @param FileHandle file handle from which size is retrieved\r
1041 @param Size pointer to size\r
1042\r
1043 @retval EFI_SUCCESS operation was completed sucessfully\r
1044 @retval EFI_DEVICE_ERROR cannot access the file\r
1045**/\r
1046EFI_STATUS\r
1047EFIAPI\r
1048ShellGetFileSize (\r
a405b86d 1049 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 1050 OUT UINT64 *Size\r
a405b86d 1051 )\r
1052{\r
d2b4564b 1053 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
94b17fa1 1054}\r
1055/**\r
1056 Retrieves the status of the break execution flag\r
1057\r
1058 this function is useful to check whether the application is being asked to halt by the shell.\r
1059\r
1060 @retval TRUE the execution break is enabled\r
1061 @retval FALSE the execution break is not enabled\r
1062**/\r
1063BOOLEAN\r
1064EFIAPI\r
1065ShellGetExecutionBreakFlag(\r
1066 VOID\r
1067 )\r
1068{\r
1e6e84c7 1069 //\r
94b17fa1 1070 // Check for UEFI Shell 2.0 protocols\r
1071 //\r
366f81a0 1072 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1073\r
1074 //\r
1075 // We are using UEFI Shell 2.0; see if the event has been triggered\r
1076 //\r
366f81a0 1077 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
94b17fa1 1078 return (FALSE);\r
1079 }\r
1080 return (TRUE);\r
1e6e84c7 1081 }\r
94b17fa1 1082\r
1083 //\r
1084 // using EFI Shell; call the function to check\r
1085 //\r
92a5447e 1086 if (mEfiShellEnvironment2 != NULL) {\r
1087 return (mEfiShellEnvironment2->GetExecutionBreak());\r
1088 }\r
1089\r
1090 return (FALSE);\r
94b17fa1 1091}\r
1092/**\r
1093 return the value of an environment variable\r
1094\r
1e6e84c7 1095 this function gets the value of the environment variable set by the\r
94b17fa1 1096 ShellSetEnvironmentVariable function\r
1097\r
1098 @param EnvKey The key name of the environment variable.\r
1099\r
1100 @retval NULL the named environment variable does not exist.\r
1101 @return != NULL pointer to the value of the environment variable\r
1102**/\r
1103CONST CHAR16*\r
1104EFIAPI\r
1105ShellGetEnvironmentVariable (\r
9b3bf083 1106 IN CONST CHAR16 *EnvKey\r
94b17fa1 1107 )\r
1108{\r
1e6e84c7 1109 //\r
94b17fa1 1110 // Check for UEFI Shell 2.0 protocols\r
1111 //\r
366f81a0 1112 if (gEfiShellProtocol != NULL) {\r
1113 return (gEfiShellProtocol->GetEnv(EnvKey));\r
94b17fa1 1114 }\r
1115\r
1116 //\r
92a5447e 1117 // Check for EFI shell\r
94b17fa1 1118 //\r
92a5447e 1119 if (mEfiShellEnvironment2 != NULL) {\r
1120 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
1121 }\r
94b17fa1 1122\r
92a5447e 1123 return NULL;\r
94b17fa1 1124}\r
1125/**\r
1126 set the value of an environment variable\r
1127\r
1128This function changes the current value of the specified environment variable. If the\r
1129environment variable exists and the Value is an empty string, then the environment\r
1130variable is deleted. If the environment variable exists and the Value is not an empty\r
1131string, then the value of the environment variable is changed. If the environment\r
1132variable does not exist and the Value is an empty string, there is no action. If the\r
1133environment variable does not exist and the Value is a non-empty string, then the\r
1134environment variable is created and assigned the specified value.\r
1135\r
1136 This is not supported pre-UEFI Shell 2.0.\r
1137\r
1138 @param EnvKey The key name of the environment variable.\r
1139 @param EnvVal The Value of the environment variable\r
1140 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1141\r
1142 @retval EFI_SUCCESS the operation was completed sucessfully\r
1143 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1144**/\r
1145EFI_STATUS\r
1146EFIAPI\r
1147ShellSetEnvironmentVariable (\r
1148 IN CONST CHAR16 *EnvKey,\r
1149 IN CONST CHAR16 *EnvVal,\r
1150 IN BOOLEAN Volatile\r
1151 )\r
1152{\r
1e6e84c7 1153 //\r
94b17fa1 1154 // Check for UEFI Shell 2.0 protocols\r
1155 //\r
366f81a0 1156 if (gEfiShellProtocol != NULL) {\r
1157 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
1e6e84c7 1158 }\r
94b17fa1 1159\r
1160 //\r
1161 // This feature does not exist under EFI shell\r
1162 //\r
1163 return (EFI_UNSUPPORTED);\r
1164}\r
a405b86d 1165\r
94b17fa1 1166/**\r
a405b86d 1167 Cause the shell to parse and execute a command line.\r
94b17fa1 1168\r
1169 This function creates a nested instance of the shell and executes the specified\r
a405b86d 1170 command (CommandLine) with the specified environment (Environment). Upon return,\r
1171 the status code returned by the specified command is placed in StatusCode.\r
1172 If Environment is NULL, then the current environment is used and all changes made\r
1173 by the commands executed will be reflected in the current environment. If the\r
1174 Environment is non-NULL, then the changes made will be discarded.\r
1175 The CommandLine is executed from the current working directory on the current\r
1176 device.\r
1177\r
3877d0f5 1178 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0\r
a405b86d 1179 environment. The values pointed to by the parameters will be unchanged by the\r
1180 ShellExecute() function. The Output parameter has no effect in a\r
1181 UEFI Shell 2.0 environment.\r
1182\r
1183 @param[in] ParentHandle The parent image starting the operation.\r
1184 @param[in] CommandLine The pointer to a NULL terminated command line.\r
1185 @param[in] Output True to display debug output. False to hide it.\r
1186 @param[in] EnvironmentVariables Optional pointer to array of environment variables\r
1187 in the form "x=y". If NULL, the current set is used.\r
1188 @param[out] Status The status of the run command line.\r
1189\r
1190 @retval EFI_SUCCESS The operation completed sucessfully. Status\r
1191 contains the status code returned.\r
1192 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.\r
1193 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1194 @retval EFI_UNSUPPORTED The operation is not allowed.\r
94b17fa1 1195**/\r
1196EFI_STATUS\r
1197EFIAPI\r
1198ShellExecute (\r
1199 IN EFI_HANDLE *ParentHandle,\r
1200 IN CHAR16 *CommandLine OPTIONAL,\r
1201 IN BOOLEAN Output OPTIONAL,\r
1202 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1203 OUT EFI_STATUS *Status OPTIONAL\r
1204 )\r
1205{\r
3877d0f5 1206 EFI_STATUS CmdStatus;\r
1e6e84c7 1207 //\r
94b17fa1 1208 // Check for UEFI Shell 2.0 protocols\r
1209 //\r
366f81a0 1210 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1211 //\r
1212 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1213 //\r
366f81a0 1214 return (gEfiShellProtocol->Execute(ParentHandle,\r
94b17fa1 1215 CommandLine,\r
1216 EnvironmentVariables,\r
1217 Status));\r
1e6e84c7 1218 }\r
92a5447e 1219\r
94b17fa1 1220 //\r
92a5447e 1221 // Check for EFI shell\r
94b17fa1 1222 //\r
92a5447e 1223 if (mEfiShellEnvironment2 != NULL) {\r
1224 //\r
3877d0f5 1225 // Call EFI Shell version.\r
92a5447e 1226 // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
1227 //\r
3877d0f5 1228 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,\r
92a5447e 1229 CommandLine,\r
1230 Output));\r
3877d0f5
BJ
1231 //\r
1232 // No Status output parameter so just use the returned status\r
1233 //\r
1234 if (Status != NULL) {\r
1235 *Status = CmdStatus;\r
1236 }\r
1237 //\r
1238 // If there was an error, we can't tell if it was from the command or from\r
1239 // the Execute() function, so we'll just assume the shell ran successfully\r
1240 // and the error came from the command.\r
1241 //\r
1242 return EFI_SUCCESS;\r
92a5447e 1243 }\r
1244\r
1245 return (EFI_UNSUPPORTED);\r
94b17fa1 1246}\r
3877d0f5 1247\r
94b17fa1 1248/**\r
1249 Retreives the current directory path\r
1250\r
1e6e84c7 1251 If the DeviceName is NULL, it returns the current device's current directory\r
1252 name. If the DeviceName is not NULL, it returns the current directory name\r
94b17fa1 1253 on specified drive.\r
1254\r
1255 @param DeviceName the name of the drive to get directory on\r
1256\r
1257 @retval NULL the directory does not exist\r
1258 @return != NULL the directory\r
1259**/\r
1260CONST CHAR16*\r
1261EFIAPI\r
1262ShellGetCurrentDir (\r
a405b86d 1263 IN CHAR16 * CONST DeviceName OPTIONAL\r
94b17fa1 1264 )\r
1265{\r
1e6e84c7 1266 //\r
94b17fa1 1267 // Check for UEFI Shell 2.0 protocols\r
1268 //\r
366f81a0 1269 if (gEfiShellProtocol != NULL) {\r
1270 return (gEfiShellProtocol->GetCurDir(DeviceName));\r
1e6e84c7 1271 }\r
92a5447e 1272\r
94b17fa1 1273 //\r
8bd282be 1274 // Check for EFI shell\r
94b17fa1 1275 //\r
8bd282be 1276 if (mEfiShellEnvironment2 != NULL) {\r
1277 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1278 }\r
1279\r
1280 return (NULL);\r
94b17fa1 1281}\r
1282/**\r
1283 sets (enabled or disabled) the page break mode\r
1284\r
1e6e84c7 1285 when page break mode is enabled the screen will stop scrolling\r
94b17fa1 1286 and wait for operator input before scrolling a subsequent screen.\r
1287\r
1288 @param CurrentState TRUE to enable and FALSE to disable\r
1289**/\r
1e6e84c7 1290VOID\r
94b17fa1 1291EFIAPI\r
1292ShellSetPageBreakMode (\r
1293 IN BOOLEAN CurrentState\r
1294 )\r
1295{\r
1296 //\r
1297 // check for enabling\r
1298 //\r
1299 if (CurrentState != 0x00) {\r
1e6e84c7 1300 //\r
94b17fa1 1301 // check for UEFI Shell 2.0\r
1302 //\r
366f81a0 1303 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1304 //\r
1305 // Enable with UEFI 2.0 Shell\r
1306 //\r
366f81a0 1307 gEfiShellProtocol->EnablePageBreak();\r
94b17fa1 1308 return;\r
1309 } else {\r
1e6e84c7 1310 //\r
92a5447e 1311 // Check for EFI shell\r
94b17fa1 1312 //\r
92a5447e 1313 if (mEfiShellEnvironment2 != NULL) {\r
1314 //\r
1315 // Enable with EFI Shell\r
1316 //\r
1317 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1318 return;\r
1319 }\r
94b17fa1 1320 }\r
1321 } else {\r
1e6e84c7 1322 //\r
94b17fa1 1323 // check for UEFI Shell 2.0\r
1324 //\r
366f81a0 1325 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1326 //\r
1327 // Disable with UEFI 2.0 Shell\r
1328 //\r
366f81a0 1329 gEfiShellProtocol->DisablePageBreak();\r
94b17fa1 1330 return;\r
1331 } else {\r
1e6e84c7 1332 //\r
92a5447e 1333 // Check for EFI shell\r
94b17fa1 1334 //\r
92a5447e 1335 if (mEfiShellEnvironment2 != NULL) {\r
1336 //\r
1337 // Disable with EFI Shell\r
1338 //\r
1339 mEfiShellEnvironment2->DisablePageBreak ();\r
1340 return;\r
1341 }\r
94b17fa1 1342 }\r
1343 }\r
1344}\r
1345\r
1346///\r
1347/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1348/// This allows for the struct to be populated.\r
1349///\r
1350typedef struct {\r
d2b4564b 1351 LIST_ENTRY Link;\r
94b17fa1 1352 EFI_STATUS Status;\r
1353 CHAR16 *FullName;\r
1354 CHAR16 *FileName;\r
a405b86d 1355 SHELL_FILE_HANDLE Handle;\r
94b17fa1 1356 EFI_FILE_INFO *Info;\r
1357} EFI_SHELL_FILE_INFO_NO_CONST;\r
1358\r
1359/**\r
1360 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1361\r
1362 if OldStyleFileList is NULL then ASSERT()\r
1363\r
1e6e84c7 1364 this function will convert a SHELL_FILE_ARG based list into a callee allocated\r
94b17fa1 1365 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1366 the ShellCloseFileMetaArg function.\r
1367\r
4ff7e37b
ED
1368 @param[in] FileList the EFI shell list type\r
1369 @param[in, out] ListHead the list to add to\r
94b17fa1 1370\r
1371 @retval the resultant head of the double linked new format list;\r
1372**/\r
1373LIST_ENTRY*\r
1374EFIAPI\r
1375InternalShellConvertFileListType (\r
9b3bf083 1376 IN LIST_ENTRY *FileList,\r
1377 IN OUT LIST_ENTRY *ListHead\r
125c2cf4 1378 )\r
1379{\r
94b17fa1 1380 SHELL_FILE_ARG *OldInfo;\r
9b3bf083 1381 LIST_ENTRY *Link;\r
94b17fa1 1382 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1383\r
1384 //\r
9b3bf083 1385 // ASSERTs\r
94b17fa1 1386 //\r
9b3bf083 1387 ASSERT(FileList != NULL);\r
1388 ASSERT(ListHead != NULL);\r
94b17fa1 1389\r
1390 //\r
1391 // enumerate through each member of the old list and copy\r
1392 //\r
d2b4564b 1393 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
94b17fa1 1394 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
a405b86d 1395 ASSERT(OldInfo != NULL);\r
1396\r
1397 //\r
1398 // Skip ones that failed to open...\r
1399 //\r
1400 if (OldInfo->Status != EFI_SUCCESS) {\r
1401 continue;\r
1402 }\r
94b17fa1 1403\r
1404 //\r
1405 // make sure the old list was valid\r
1406 //\r
94b17fa1 1407 ASSERT(OldInfo->Info != NULL);\r
1408 ASSERT(OldInfo->FullName != NULL);\r
1409 ASSERT(OldInfo->FileName != NULL);\r
1410\r
1411 //\r
1412 // allocate a new EFI_SHELL_FILE_INFO object\r
1413 //\r
1414 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
c9d92df0 1415 if (NewInfo == NULL) {\r
7a95efda 1416 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));\r
beab0fc5 1417 ListHead = NULL;\r
c9d92df0 1418 break;\r
1419 }\r
1e6e84c7 1420\r
1421 //\r
94b17fa1 1422 // copy the simple items\r
1423 //\r
1424 NewInfo->Handle = OldInfo->Handle;\r
1425 NewInfo->Status = OldInfo->Status;\r
1426\r
d2b4564b 1427 // old shell checks for 0 not NULL\r
1428 OldInfo->Handle = 0;\r
1429\r
94b17fa1 1430 //\r
1431 // allocate new space to copy strings and structure\r
1432 //\r
98c16be5
JC
1433 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);\r
1434 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);\r
1435 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);\r
1e6e84c7 1436\r
94b17fa1 1437 //\r
1438 // make sure all the memory allocations were sucessful\r
1439 //\r
beab0fc5 1440 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {\r
98c16be5
JC
1441 //\r
1442 // Free the partially allocated new node\r
1443 //\r
1444 SHELL_FREE_NON_NULL(NewInfo->FullName);\r
1445 SHELL_FREE_NON_NULL(NewInfo->FileName);\r
1446 SHELL_FREE_NON_NULL(NewInfo->Info);\r
1447 SHELL_FREE_NON_NULL(NewInfo);\r
1448\r
1449 //\r
1450 // Free the previously converted stuff\r
1451 //\r
7a95efda 1452 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));\r
beab0fc5 1453 ListHead = NULL;\r
1454 break;\r
1455 }\r
94b17fa1 1456\r
94b17fa1 1457 //\r
1458 // add that to the list\r
1459 //\r
9b3bf083 1460 InsertTailList(ListHead, &NewInfo->Link);\r
94b17fa1 1461 }\r
1462 return (ListHead);\r
1463}\r
1464/**\r
1465 Opens a group of files based on a path.\r
1466\r
1e6e84c7 1467 This function uses the Arg to open all the matching files. Each matched\r
70879314
BJ
1468 file has a SHELL_FILE_INFO structure to record the file information. These\r
1469 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO\r
94b17fa1 1470 structures from ListHead to access each file. This function supports wildcards\r
1e6e84c7 1471 and will process '?' and '*' as such. the list must be freed with a call to\r
94b17fa1 1472 ShellCloseFileMetaArg().\r
1473\r
1e6e84c7 1474 If you are NOT appending to an existing list *ListHead must be NULL. If\r
5f7431d0 1475 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1476\r
1477 @param Arg pointer to path string\r
1478 @param OpenMode mode to open files with\r
1479 @param ListHead head of linked list of results\r
1480\r
1e6e84c7 1481 @retval EFI_SUCCESS the operation was sucessful and the list head\r
94b17fa1 1482 contains the list of opened files\r
94b17fa1 1483 @return != EFI_SUCCESS the operation failed\r
1484\r
1485 @sa InternalShellConvertFileListType\r
1486**/\r
1487EFI_STATUS\r
1488EFIAPI\r
1489ShellOpenFileMetaArg (\r
1490 IN CHAR16 *Arg,\r
1491 IN UINT64 OpenMode,\r
1492 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1493 )\r
1494{\r
1495 EFI_STATUS Status;\r
9b3bf083 1496 LIST_ENTRY mOldStyleFileList;\r
1e6e84c7 1497\r
94b17fa1 1498 //\r
1499 // ASSERT that Arg and ListHead are not NULL\r
1500 //\r
1501 ASSERT(Arg != NULL);\r
1502 ASSERT(ListHead != NULL);\r
1503\r
1e6e84c7 1504 //\r
94b17fa1 1505 // Check for UEFI Shell 2.0 protocols\r
1506 //\r
366f81a0 1507 if (gEfiShellProtocol != NULL) {\r
5f7431d0 1508 if (*ListHead == NULL) {\r
1509 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1510 if (*ListHead == NULL) {\r
1511 return (EFI_OUT_OF_RESOURCES);\r
1512 }\r
1513 InitializeListHead(&((*ListHead)->Link));\r
1e6e84c7 1514 }\r
366f81a0 1515 Status = gEfiShellProtocol->OpenFileList(Arg,\r
1e6e84c7 1516 OpenMode,\r
2247dde4 1517 ListHead);\r
1518 if (EFI_ERROR(Status)) {\r
366f81a0 1519 gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
2247dde4 1520 } else {\r
366f81a0 1521 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
2247dde4 1522 }\r
a405b86d 1523 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {\r
1524 FreePool(*ListHead);\r
1525 *ListHead = NULL;\r
1526 return (EFI_NOT_FOUND);\r
1527 }\r
2247dde4 1528 return (Status);\r
1e6e84c7 1529 }\r
94b17fa1 1530\r
1531 //\r
92a5447e 1532 // Check for EFI shell\r
94b17fa1 1533 //\r
92a5447e 1534 if (mEfiShellEnvironment2 != NULL) {\r
1535 //\r
1536 // make sure the list head is initialized\r
1537 //\r
1538 InitializeListHead(&mOldStyleFileList);\r
94b17fa1 1539\r
92a5447e 1540 //\r
1541 // Get the EFI Shell list of files\r
1542 //\r
1543 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
1544 if (EFI_ERROR(Status)) {\r
1545 *ListHead = NULL;\r
1546 return (Status);\r
1547 }\r
94b17fa1 1548\r
9b3bf083 1549 if (*ListHead == NULL) {\r
92a5447e 1550 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1551 if (*ListHead == NULL) {\r
1552 return (EFI_OUT_OF_RESOURCES);\r
1553 }\r
1554 InitializeListHead(&((*ListHead)->Link));\r
9b3bf083 1555 }\r
9b3bf083 1556\r
92a5447e 1557 //\r
1558 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1559 //\r
1560 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1561\r
92a5447e 1562 //\r
1563 // Free the EFI Shell version that was converted.\r
1564 //\r
1565 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
94b17fa1 1566\r
92a5447e 1567 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
1568 FreePool(*ListHead);\r
1569 *ListHead = NULL;\r
1570 Status = EFI_NOT_FOUND;\r
1571 }\r
1572 return (Status);\r
a405b86d 1573 }\r
1574\r
92a5447e 1575 return (EFI_UNSUPPORTED);\r
94b17fa1 1576}\r
1577/**\r
a405b86d 1578 Free the linked list returned from ShellOpenFileMetaArg.\r
94b17fa1 1579\r
a405b86d 1580 if ListHead is NULL then ASSERT().\r
94b17fa1 1581\r
a405b86d 1582 @param ListHead the pointer to free.\r
94b17fa1 1583\r
a405b86d 1584 @retval EFI_SUCCESS the operation was sucessful.\r
94b17fa1 1585**/\r
1586EFI_STATUS\r
1587EFIAPI\r
1588ShellCloseFileMetaArg (\r
1589 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1590 )\r
1591{\r
1592 LIST_ENTRY *Node;\r
1593\r
1594 //\r
1595 // ASSERT that ListHead is not NULL\r
1596 //\r
1597 ASSERT(ListHead != NULL);\r
1598\r
1e6e84c7 1599 //\r
94b17fa1 1600 // Check for UEFI Shell 2.0 protocols\r
1601 //\r
366f81a0 1602 if (gEfiShellProtocol != NULL) {\r
1603 return (gEfiShellProtocol->FreeFileList(ListHead));\r
92a5447e 1604 } else if (mEfiShellEnvironment2 != NULL) {\r
94b17fa1 1605 //\r
1e6e84c7 1606 // Since this is EFI Shell version we need to free our internally made copy\r
94b17fa1 1607 // of the list\r
1608 //\r
1e6e84c7 1609 for ( Node = GetFirstNode(&(*ListHead)->Link)\r
a405b86d 1610 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)\r
9b3bf083 1611 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
94b17fa1 1612 RemoveEntryList(Node);\r
a405b86d 1613 ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
94b17fa1 1614 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1615 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1616 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1617 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1618 }\r
75a5e2ef 1619 SHELL_FREE_NON_NULL(*ListHead);\r
94b17fa1 1620 return EFI_SUCCESS;\r
1621 }\r
92a5447e 1622\r
1623 return (EFI_UNSUPPORTED);\r
94b17fa1 1624}\r
1625\r
125c2cf4 1626/**\r
1627 Find a file by searching the CWD and then the path.\r
1628\r
b3011f40 1629 If FileName is NULL then ASSERT.\r
125c2cf4 1630\r
b3011f40 1631 If the return value is not NULL then the memory must be caller freed.\r
125c2cf4 1632\r
1633 @param FileName Filename string.\r
1634\r
1635 @retval NULL the file was not found\r
1636 @return !NULL the full path to the file.\r
1637**/\r
1638CHAR16 *\r
1639EFIAPI\r
1640ShellFindFilePath (\r
1641 IN CONST CHAR16 *FileName\r
1642 )\r
1643{\r
1644 CONST CHAR16 *Path;\r
a405b86d 1645 SHELL_FILE_HANDLE Handle;\r
125c2cf4 1646 EFI_STATUS Status;\r
1647 CHAR16 *RetVal;\r
1648 CHAR16 *TestPath;\r
1649 CONST CHAR16 *Walker;\r
36a9d672 1650 UINTN Size;\r
1cd45e78 1651 CHAR16 *TempChar;\r
125c2cf4 1652\r
1653 RetVal = NULL;\r
1654\r
a405b86d 1655 //\r
1656 // First make sure its not an absolute path.\r
1657 //\r
1658 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);\r
1659 if (!EFI_ERROR(Status)){\r
1660 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1661 ASSERT(RetVal == NULL);\r
1662 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);\r
1663 ShellCloseFile(&Handle);\r
1664 return (RetVal);\r
1665 } else {\r
1666 ShellCloseFile(&Handle);\r
1667 }\r
1668 }\r
1669\r
125c2cf4 1670 Path = ShellGetEnvironmentVariable(L"cwd");\r
1671 if (Path != NULL) {\r
36a9d672 1672 Size = StrSize(Path);\r
1673 Size += StrSize(FileName);\r
1674 TestPath = AllocateZeroPool(Size);\r
c9d92df0 1675 if (TestPath == NULL) {\r
1676 return (NULL);\r
1677 }\r
98c16be5
JC
1678 StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1);\r
1679 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));\r
125c2cf4 1680 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1681 if (!EFI_ERROR(Status)){\r
a405b86d 1682 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1683 ASSERT(RetVal == NULL);\r
1684 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1685 ShellCloseFile(&Handle);\r
1686 FreePool(TestPath);\r
1687 return (RetVal);\r
1688 } else {\r
1689 ShellCloseFile(&Handle);\r
1690 }\r
125c2cf4 1691 }\r
1692 FreePool(TestPath);\r
1693 }\r
1694 Path = ShellGetEnvironmentVariable(L"path");\r
1695 if (Path != NULL) {\r
a405b86d 1696 Size = StrSize(Path)+sizeof(CHAR16);\r
36a9d672 1697 Size += StrSize(FileName);\r
1698 TestPath = AllocateZeroPool(Size);\r
3e082d58 1699 if (TestPath == NULL) {\r
1700 return (NULL);\r
1701 }\r
1e6e84c7 1702 Walker = (CHAR16*)Path;\r
125c2cf4 1703 do {\r
1704 CopyMem(TestPath, Walker, StrSize(Walker));\r
3e082d58 1705 if (TestPath != NULL) {\r
1706 TempChar = StrStr(TestPath, L";");\r
1707 if (TempChar != NULL) {\r
1708 *TempChar = CHAR_NULL;\r
1709 }\r
1710 if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
98c16be5 1711 StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath));\r
3e082d58 1712 }\r
89e8537a 1713 if (FileName[0] == L'\\') {\r
1714 FileName++;\r
1715 }\r
98c16be5 1716 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));\r
3e082d58 1717 if (StrStr(Walker, L";") != NULL) {\r
1718 Walker = StrStr(Walker, L";") + 1;\r
a405b86d 1719 } else {\r
3e082d58 1720 Walker = NULL;\r
1721 }\r
1722 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1723 if (!EFI_ERROR(Status)){\r
1724 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1725 ASSERT(RetVal == NULL);\r
1726 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1727 ShellCloseFile(&Handle);\r
1728 break;\r
1729 } else {\r
1730 ShellCloseFile(&Handle);\r
1731 }\r
a405b86d 1732 }\r
125c2cf4 1733 }\r
1734 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1735 FreePool(TestPath);\r
1736 }\r
1737 return (RetVal);\r
1738}\r
1739\r
b3011f40 1740/**\r
1e6e84c7 1741 Find a file by searching the CWD and then the path with a variable set of file\r
1742 extensions. If the file is not found it will append each extension in the list\r
b3011f40 1743 in the order provided and return the first one that is successful.\r
1744\r
1745 If FileName is NULL, then ASSERT.\r
1746 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1747\r
1748 If the return value is not NULL then the memory must be caller freed.\r
1749\r
1750 @param[in] FileName Filename string.\r
1751 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1752\r
1753 @retval NULL The file was not found.\r
1754 @retval !NULL The path to the file.\r
1755**/\r
1756CHAR16 *\r
1757EFIAPI\r
1758ShellFindFilePathEx (\r
1759 IN CONST CHAR16 *FileName,\r
1760 IN CONST CHAR16 *FileExtension\r
1761 )\r
1762{\r
1763 CHAR16 *TestPath;\r
1764 CHAR16 *RetVal;\r
1765 CONST CHAR16 *ExtensionWalker;\r
9e926b69 1766 UINTN Size;\r
1cd45e78 1767 CHAR16 *TempChar;\r
c9d92df0 1768 CHAR16 *TempChar2;\r
1cd45e78 1769\r
b3011f40 1770 ASSERT(FileName != NULL);\r
1771 if (FileExtension == NULL) {\r
1772 return (ShellFindFilePath(FileName));\r
1773 }\r
1774 RetVal = ShellFindFilePath(FileName);\r
1775 if (RetVal != NULL) {\r
1776 return (RetVal);\r
1777 }\r
9e926b69 1778 Size = StrSize(FileName);\r
1779 Size += StrSize(FileExtension);\r
1780 TestPath = AllocateZeroPool(Size);\r
c9d92df0 1781 if (TestPath == NULL) {\r
1782 return (NULL);\r
1783 }\r
a405b86d 1784 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){\r
98c16be5 1785 StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1);\r
a405b86d 1786 if (ExtensionWalker != NULL) {\r
98c16be5 1787 StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));\r
a405b86d 1788 }\r
1cd45e78 1789 TempChar = StrStr(TestPath, L";");\r
1790 if (TempChar != NULL) {\r
1791 *TempChar = CHAR_NULL;\r
b3011f40 1792 }\r
1793 RetVal = ShellFindFilePath(TestPath);\r
1794 if (RetVal != NULL) {\r
1795 break;\r
1796 }\r
a405b86d 1797 ASSERT(ExtensionWalker != NULL);\r
c9d92df0 1798 TempChar2 = StrStr(ExtensionWalker, L";");\r
b3011f40 1799 }\r
1800 FreePool(TestPath);\r
1801 return (RetVal);\r
1802}\r
1803\r
94b17fa1 1804typedef struct {\r
9b3bf083 1805 LIST_ENTRY Link;\r
94b17fa1 1806 CHAR16 *Name;\r
a405b86d 1807 SHELL_PARAM_TYPE Type;\r
94b17fa1 1808 CHAR16 *Value;\r
1809 UINTN OriginalPosition;\r
1810} SHELL_PARAM_PACKAGE;\r
1811\r
1812/**\r
1e6e84c7 1813 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
94b17fa1 1814 return value is TRUE then the type parameter is set also.\r
1e6e84c7 1815\r
94b17fa1 1816 if CheckList is NULL then ASSERT();\r
1817 if Name is NULL then ASSERT();\r
1818 if Type is NULL then ASSERT();\r
1819\r
94b17fa1 1820 @param Name pointer to Name of parameter found\r
1821 @param CheckList List to check against\r
a405b86d 1822 @param Type pointer to type of parameter if it was found\r
94b17fa1 1823\r
1824 @retval TRUE the Parameter was found. Type is valid.\r
1825 @retval FALSE the Parameter was not found. Type is not valid.\r
1826**/\r
1827BOOLEAN\r
1828EFIAPI\r
d2b4564b 1829InternalIsOnCheckList (\r
94b17fa1 1830 IN CONST CHAR16 *Name,\r
1831 IN CONST SHELL_PARAM_ITEM *CheckList,\r
252d9457 1832 OUT SHELL_PARAM_TYPE *Type\r
a405b86d 1833 )\r
1834{\r
94b17fa1 1835 SHELL_PARAM_ITEM *TempListItem;\r
252d9457 1836 CHAR16 *TempString;\r
94b17fa1 1837\r
1838 //\r
1839 // ASSERT that all 3 pointer parameters aren't NULL\r
1840 //\r
1841 ASSERT(CheckList != NULL);\r
1842 ASSERT(Type != NULL);\r
1843 ASSERT(Name != NULL);\r
1844\r
d2b4564b 1845 //\r
1846 // question mark and page break mode are always supported\r
1847 //\r
1848 if ((StrCmp(Name, L"-?") == 0) ||\r
1849 (StrCmp(Name, L"-b") == 0)\r
a405b86d 1850 ) {\r
252d9457 1851 *Type = TypeFlag;\r
d2b4564b 1852 return (TRUE);\r
1853 }\r
1854\r
94b17fa1 1855 //\r
1856 // Enumerate through the list\r
1857 //\r
1858 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1859 //\r
9eb53ac3 1860 // If the Type is TypeStart only check the first characters of the passed in param\r
1861 // If it matches set the type and return TRUE\r
94b17fa1 1862 //\r
b0934ac4 1863 if (TempListItem->Type == TypeStart) {\r
252d9457 1864 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1865 *Type = TempListItem->Type;\r
1866 return (TRUE);\r
1867 }\r
1868 TempString = NULL;\r
1869 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));\r
1870 if (TempString != NULL) {\r
1871 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {\r
1872 *Type = TempListItem->Type;\r
1873 FreePool(TempString);\r
1874 return (TRUE);\r
1875 }\r
1876 FreePool(TempString);\r
1877 }\r
1878 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {\r
94b17fa1 1879 *Type = TempListItem->Type;\r
1880 return (TRUE);\r
1881 }\r
1882 }\r
2247dde4 1883\r
94b17fa1 1884 return (FALSE);\r
1885}\r
1886/**\r
d2b4564b 1887 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 1888\r
a405b86d 1889 @param[in] Name pointer to Name of parameter found\r
1890 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.\r
94b17fa1 1891\r
1892 @retval TRUE the Parameter is a flag.\r
a405b86d 1893 @retval FALSE the Parameter not a flag.\r
94b17fa1 1894**/\r
1895BOOLEAN\r
1896EFIAPI\r
d2b4564b 1897InternalIsFlag (\r
2247dde4 1898 IN CONST CHAR16 *Name,\r
1899 IN BOOLEAN AlwaysAllowNumbers\r
94b17fa1 1900 )\r
1901{\r
1902 //\r
1903 // ASSERT that Name isn't NULL\r
1904 //\r
1905 ASSERT(Name != NULL);\r
1906\r
2247dde4 1907 //\r
1908 // If we accept numbers then dont return TRUE. (they will be values)\r
1909 //\r
8af89dae 1910 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE)) && AlwaysAllowNumbers) {\r
2247dde4 1911 return (FALSE);\r
1912 }\r
1913\r
94b17fa1 1914 //\r
a405b86d 1915 // If the Name has a /, +, or - as the first character return TRUE\r
94b17fa1 1916 //\r
1e6e84c7 1917 if ((Name[0] == L'/') ||\r
d2b4564b 1918 (Name[0] == L'-') ||\r
1919 (Name[0] == L'+')\r
a405b86d 1920 ) {\r
94b17fa1 1921 return (TRUE);\r
1922 }\r
1923 return (FALSE);\r
1924}\r
1925\r
1926/**\r
1e6e84c7 1927 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 1928\r
1929 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 1930\r
a405b86d 1931 @param[in] CheckList pointer to list of parameters to check\r
1932 @param[out] CheckPackage pointer to pointer to list checked values\r
1933 @param[out] ProblemParam optional pointer to pointer to unicode string for\r
d2b4564b 1934 the paramater that caused failure. If used then the\r
1935 caller is responsible for freeing the memory.\r
a405b86d 1936 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1937 @param[in] Argv pointer to array of parameters\r
1938 @param[in] Argc Count of parameters in Argv\r
1939 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.\r
94b17fa1 1940\r
1941 @retval EFI_SUCCESS The operation completed sucessfully.\r
1942 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1943 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1e6e84c7 1944 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1945 duplicated. the duplicated command line argument\r
94b17fa1 1946 was returned in ProblemParam if provided.\r
1e6e84c7 1947 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
94b17fa1 1948 the invalid command line argument was returned in\r
1949 ProblemParam if provided.\r
1950**/\r
1951EFI_STATUS\r
1952EFIAPI\r
1953InternalCommandLineParse (\r
1954 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1955 OUT LIST_ENTRY **CheckPackage,\r
1956 OUT CHAR16 **ProblemParam OPTIONAL,\r
1957 IN BOOLEAN AutoPageBreak,\r
1958 IN CONST CHAR16 **Argv,\r
2247dde4 1959 IN UINTN Argc,\r
1960 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 1961 )\r
1962{\r
94b17fa1 1963 UINTN LoopCounter;\r
252d9457 1964 SHELL_PARAM_TYPE CurrentItemType;\r
94b17fa1 1965 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
125c2cf4 1966 UINTN GetItemValue;\r
1967 UINTN ValueSize;\r
a405b86d 1968 UINTN Count;\r
252d9457 1969 CONST CHAR16 *TempPointer;\r
98c16be5 1970 UINTN CurrentValueSize;\r
94b17fa1 1971\r
1972 CurrentItemPackage = NULL;\r
125c2cf4 1973 GetItemValue = 0;\r
1974 ValueSize = 0;\r
a405b86d 1975 Count = 0;\r
94b17fa1 1976\r
1977 //\r
1978 // If there is only 1 item we dont need to do anything\r
1979 //\r
a405b86d 1980 if (Argc < 1) {\r
94b17fa1 1981 *CheckPackage = NULL;\r
1982 return (EFI_SUCCESS);\r
1983 }\r
1984\r
2247dde4 1985 //\r
1986 // ASSERTs\r
1987 //\r
1988 ASSERT(CheckList != NULL);\r
1989 ASSERT(Argv != NULL);\r
1990\r
94b17fa1 1991 //\r
1992 // initialize the linked list\r
1993 //\r
1994 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
02a758cb 1995 if (*CheckPackage == NULL) {\r
beab0fc5 1996 return (EFI_OUT_OF_RESOURCES);\r
02a758cb 1997 }\r
beab0fc5 1998\r
94b17fa1 1999 InitializeListHead(*CheckPackage);\r
2000\r
2001 //\r
2002 // loop through each of the arguments\r
2003 //\r
2004 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
2005 if (Argv[LoopCounter] == NULL) {\r
2006 //\r
2007 // do nothing for NULL argv\r
2008 //\r
a405b86d 2009 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
2247dde4 2010 //\r
2011 // We might have leftover if last parameter didnt have optional value\r
2012 //\r
125c2cf4 2013 if (GetItemValue != 0) {\r
2014 GetItemValue = 0;\r
2247dde4 2015 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2016 }\r
94b17fa1 2017 //\r
2018 // this is a flag\r
2019 //\r
252d9457 2020 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 2021 if (CurrentItemPackage == NULL) {\r
2022 ShellCommandLineFreeVarList(*CheckPackage);\r
2023 *CheckPackage = NULL;\r
2024 return (EFI_OUT_OF_RESOURCES);\r
2025 }\r
98c16be5 2026 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);\r
beab0fc5 2027 if (CurrentItemPackage->Name == NULL) {\r
2028 ShellCommandLineFreeVarList(*CheckPackage);\r
2029 *CheckPackage = NULL;\r
2030 return (EFI_OUT_OF_RESOURCES);\r
2031 }\r
94b17fa1 2032 CurrentItemPackage->Type = CurrentItemType;\r
2033 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 2034 CurrentItemPackage->Value = NULL;\r
94b17fa1 2035\r
2036 //\r
2037 // Does this flag require a value\r
2038 //\r
125c2cf4 2039 switch (CurrentItemPackage->Type) {\r
94b17fa1 2040 //\r
125c2cf4 2041 // possibly trigger the next loop(s) to populate the value of this item\r
1e6e84c7 2042 //\r
125c2cf4 2043 case TypeValue:\r
1e6e84c7 2044 GetItemValue = 1;\r
125c2cf4 2045 ValueSize = 0;\r
2046 break;\r
2047 case TypeDoubleValue:\r
2048 GetItemValue = 2;\r
2049 ValueSize = 0;\r
2050 break;\r
2051 case TypeMaxValue:\r
2052 GetItemValue = (UINTN)(-1);\r
2053 ValueSize = 0;\r
2054 break;\r
2055 default:\r
2056 //\r
2057 // this item has no value expected; we are done\r
2058 //\r
2059 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2060 ASSERT(GetItemValue == 0);\r
2061 break;\r
94b17fa1 2062 }\r
a405b86d 2063 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {\r
b1f95a06 2064 ASSERT(CurrentItemPackage != NULL);\r
2065 //\r
125c2cf4 2066 // get the item VALUE for a previous flag\r
b1f95a06 2067 //\r
49bd498d 2068 if (StrStr(Argv[LoopCounter], L" ") == NULL) {\r
98c16be5
JC
2069 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
2070 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
49bd498d 2071 ASSERT(CurrentItemPackage->Value != NULL);\r
2072 if (ValueSize == 0) {\r
98c16be5 2073 StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1);\r
49bd498d 2074 } else {\r
98c16be5
JC
2075 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
2076 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
49bd498d 2077 }\r
2078 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
125c2cf4 2079 } else {\r
49bd498d 2080 //\r
2081 // the parameter has spaces. must be quoted.\r
2082 //\r
98c16be5
JC
2083 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16);\r
2084 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
49bd498d 2085 ASSERT(CurrentItemPackage->Value != NULL);\r
2086 if (ValueSize == 0) {\r
98c16be5
JC
2087 StrnCpy(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1);\r
2088 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
2089 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
49bd498d 2090 } else {\r
98c16be5
JC
2091 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
2092 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
2093 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
2094 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));\r
49bd498d 2095 }\r
2096 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
125c2cf4 2097 }\r
125c2cf4 2098 GetItemValue--;\r
2099 if (GetItemValue == 0) {\r
2100 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2101 }\r
a405b86d 2102 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {\r
b1f95a06 2103 //\r
2104 // add this one as a non-flag\r
2105 //\r
252d9457 2106\r
2107 TempPointer = Argv[LoopCounter];\r
b0934ac4 2108 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')\r
252d9457 2109 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
2110 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
2111 ){\r
2112 TempPointer++;\r
2113 }\r
2114 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 2115 if (CurrentItemPackage == NULL) {\r
2116 ShellCommandLineFreeVarList(*CheckPackage);\r
2117 *CheckPackage = NULL;\r
2118 return (EFI_OUT_OF_RESOURCES);\r
2119 }\r
b1f95a06 2120 CurrentItemPackage->Name = NULL;\r
2121 CurrentItemPackage->Type = TypePosition;\r
98c16be5 2122 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);\r
beab0fc5 2123 if (CurrentItemPackage->Value == NULL) {\r
2124 ShellCommandLineFreeVarList(*CheckPackage);\r
2125 *CheckPackage = NULL;\r
2126 return (EFI_OUT_OF_RESOURCES);\r
2127 }\r
a405b86d 2128 CurrentItemPackage->OriginalPosition = Count++;\r
9b3bf083 2129 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
252d9457 2130 } else {\r
94b17fa1 2131 //\r
2132 // this was a non-recognised flag... error!\r
2133 //\r
252d9457 2134 if (ProblemParam != NULL) {\r
98c16be5 2135 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);\r
252d9457 2136 }\r
94b17fa1 2137 ShellCommandLineFreeVarList(*CheckPackage);\r
2138 *CheckPackage = NULL;\r
2139 return (EFI_VOLUME_CORRUPTED);\r
94b17fa1 2140 }\r
2141 }\r
125c2cf4 2142 if (GetItemValue != 0) {\r
2143 GetItemValue = 0;\r
2144 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2145 }\r
94b17fa1 2146 //\r
2147 // support for AutoPageBreak\r
2148 //\r
2149 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
2150 ShellSetPageBreakMode(TRUE);\r
2151 }\r
2152 return (EFI_SUCCESS);\r
2153}\r
2154\r
2155/**\r
1e6e84c7 2156 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 2157 Optionally removes NULL values first.\r
1e6e84c7 2158\r
94b17fa1 2159 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 2160\r
a405b86d 2161 @param[in] CheckList The pointer to list of parameters to check.\r
2162 @param[out] CheckPackage The package of checked values.\r
2163 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
94b17fa1 2164 the paramater that caused failure.\r
a405b86d 2165 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2166 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
94b17fa1 2167\r
2168 @retval EFI_SUCCESS The operation completed sucessfully.\r
a405b86d 2169 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2170 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2171 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2172 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
1e6e84c7 2173 of the command line arguments was returned in\r
94b17fa1 2174 ProblemParam if provided.\r
a405b86d 2175 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2176 The invalid command line argument was returned in\r
94b17fa1 2177 ProblemParam if provided.\r
2178**/\r
2179EFI_STATUS\r
2180EFIAPI\r
2247dde4 2181ShellCommandLineParseEx (\r
94b17fa1 2182 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2183 OUT LIST_ENTRY **CheckPackage,\r
2184 OUT CHAR16 **ProblemParam OPTIONAL,\r
2247dde4 2185 IN BOOLEAN AutoPageBreak,\r
2186 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 2187 )\r
2188{\r
1e6e84c7 2189 //\r
94b17fa1 2190 // ASSERT that CheckList and CheckPackage aren't NULL\r
2191 //\r
2192 ASSERT(CheckList != NULL);\r
2193 ASSERT(CheckPackage != NULL);\r
2194\r
1e6e84c7 2195 //\r
94b17fa1 2196 // Check for UEFI Shell 2.0 protocols\r
2197 //\r
366f81a0 2198 if (gEfiShellParametersProtocol != NULL) {\r
1e6e84c7 2199 return (InternalCommandLineParse(CheckList,\r
2200 CheckPackage,\r
2201 ProblemParam,\r
2202 AutoPageBreak,\r
366f81a0 2203 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,\r
2204 gEfiShellParametersProtocol->Argc,\r
2247dde4 2205 AlwaysAllowNumbers));\r
94b17fa1 2206 }\r
2207\r
1e6e84c7 2208 //\r
94b17fa1 2209 // ASSERT That EFI Shell is not required\r
2210 //\r
2211 ASSERT (mEfiShellInterface != NULL);\r
1e6e84c7 2212 return (InternalCommandLineParse(CheckList,\r
2213 CheckPackage,\r
2214 ProblemParam,\r
2215 AutoPageBreak,\r
08d7f8e8 2216 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2247dde4 2217 mEfiShellInterface->Argc,\r
2218 AlwaysAllowNumbers));\r
94b17fa1 2219}\r
2220\r
2221/**\r
2222 Frees shell variable list that was returned from ShellCommandLineParse.\r
2223\r
2224 This function will free all the memory that was used for the CheckPackage\r
2225 list of postprocessed shell arguments.\r
2226\r
2227 this function has no return value.\r
2228\r
2229 if CheckPackage is NULL, then return\r
2230\r
2231 @param CheckPackage the list to de-allocate\r
2232 **/\r
2233VOID\r
2234EFIAPI\r
2235ShellCommandLineFreeVarList (\r
2236 IN LIST_ENTRY *CheckPackage\r
a405b86d 2237 )\r
2238{\r
94b17fa1 2239 LIST_ENTRY *Node;\r
2240\r
2241 //\r
2242 // check for CheckPackage == NULL\r
2243 //\r
2244 if (CheckPackage == NULL) {\r
2245 return;\r
2246 }\r
2247\r
2248 //\r
2249 // for each node in the list\r
2250 //\r
9eb53ac3 2251 for ( Node = GetFirstNode(CheckPackage)\r
a405b86d 2252 ; !IsListEmpty(CheckPackage)\r
9eb53ac3 2253 ; Node = GetFirstNode(CheckPackage)\r
a405b86d 2254 ){\r
94b17fa1 2255 //\r
2256 // Remove it from the list\r
2257 //\r
2258 RemoveEntryList(Node);\r
2259\r
2260 //\r
2261 // if it has a name free the name\r
2262 //\r
2263 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2264 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2265 }\r
2266\r
2267 //\r
2268 // if it has a value free the value\r
2269 //\r
2270 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2271 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2272 }\r
1e6e84c7 2273\r
94b17fa1 2274 //\r
2275 // free the node structure\r
2276 //\r
2277 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2278 }\r
2279 //\r
2280 // free the list head node\r
2281 //\r
2282 FreePool(CheckPackage);\r
2283}\r
2284/**\r
2285 Checks for presence of a flag parameter\r
2286\r
2287 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2288\r
2289 if CheckPackage is NULL then return FALSE.\r
2290 if KeyString is NULL then ASSERT()\r
1e6e84c7 2291\r
94b17fa1 2292 @param CheckPackage The package of parsed command line arguments\r
2293 @param KeyString the Key of the command line argument to check for\r
2294\r
2295 @retval TRUE the flag is on the command line\r
2296 @retval FALSE the flag is not on the command line\r
2297 **/\r
2298BOOLEAN\r
2299EFIAPI\r
2300ShellCommandLineGetFlag (\r
a405b86d 2301 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2302 IN CONST CHAR16 * CONST KeyString\r
2303 )\r
2304{\r
94b17fa1 2305 LIST_ENTRY *Node;\r
252d9457 2306 CHAR16 *TempString;\r
94b17fa1 2307\r
2308 //\r
0c1950ba 2309 // return FALSE for no package or KeyString is NULL\r
94b17fa1 2310 //\r
0c1950ba 2311 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2312 return (FALSE);\r
2313 }\r
2314\r
2315 //\r
2316 // enumerate through the list of parametrs\r
2317 //\r
1e6e84c7 2318 for ( Node = GetFirstNode(CheckPackage)\r
2319 ; !IsNull (CheckPackage, Node)\r
2320 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2321 ){\r
94b17fa1 2322 //\r
2323 // If the Name matches, return TRUE (and there may be NULL name)\r
2324 //\r
2325 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2326 //\r
2327 // If Type is TypeStart then only compare the begining of the strings\r
2328 //\r
252d9457 2329 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2330 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2331 return (TRUE);\r
2332 }\r
2333 TempString = NULL;\r
2334 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2335 if (TempString != NULL) {\r
2336 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2337 FreePool(TempString);\r
2338 return (TRUE);\r
2339 }\r
2340 FreePool(TempString);\r
2341 }\r
2342 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2343 return (TRUE);\r
2344 }\r
2345 }\r
2346 }\r
2347 return (FALSE);\r
2348}\r
2349/**\r
a405b86d 2350 Returns value from command line argument.\r
94b17fa1 2351\r
a405b86d 2352 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
1e6e84c7 2353\r
a405b86d 2354 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2355\r
a405b86d 2356 @param[in] CheckPackage The package of parsed command line arguments.\r
2357 @param[in] KeyString The Key of the command line argument to check for.\r
94b17fa1 2358\r
a405b86d 2359 @retval NULL The flag is not on the command line.\r
2360 @retval !=NULL The pointer to unicode string of the value.\r
2361**/\r
94b17fa1 2362CONST CHAR16*\r
2363EFIAPI\r
2364ShellCommandLineGetValue (\r
2365 IN CONST LIST_ENTRY *CheckPackage,\r
2366 IN CHAR16 *KeyString\r
a405b86d 2367 )\r
2368{\r
94b17fa1 2369 LIST_ENTRY *Node;\r
252d9457 2370 CHAR16 *TempString;\r
94b17fa1 2371\r
2372 //\r
0c1950ba 2373 // return NULL for no package or KeyString is NULL\r
94b17fa1 2374 //\r
0c1950ba 2375 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2376 return (NULL);\r
2377 }\r
2378\r
2379 //\r
2380 // enumerate through the list of parametrs\r
2381 //\r
1e6e84c7 2382 for ( Node = GetFirstNode(CheckPackage)\r
2383 ; !IsNull (CheckPackage, Node)\r
2384 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2385 ){\r
94b17fa1 2386 //\r
252d9457 2387 // If the Name matches, return TRUE (and there may be NULL name)\r
94b17fa1 2388 //\r
2389 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2390 //\r
2391 // If Type is TypeStart then only compare the begining of the strings\r
2392 //\r
252d9457 2393 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2394 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2395 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2396 }\r
2397 TempString = NULL;\r
2398 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2399 if (TempString != NULL) {\r
2400 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2401 FreePool(TempString);\r
2402 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2403 }\r
2404 FreePool(TempString);\r
2405 }\r
2406 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2407 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2408 }\r
2409 }\r
2410 }\r
2411 return (NULL);\r
2412}\r
a405b86d 2413\r
94b17fa1 2414/**\r
a405b86d 2415 Returns raw value from command line argument.\r
94b17fa1 2416\r
a405b86d 2417 Raw value parameters are in the form of "value" in a specific position in the list.\r
1e6e84c7 2418\r
a405b86d 2419 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2420\r
a405b86d 2421 @param[in] CheckPackage The package of parsed command line arguments.\r
2422 @param[in] Position The position of the value.\r
94b17fa1 2423\r
a405b86d 2424 @retval NULL The flag is not on the command line.\r
2425 @retval !=NULL The pointer to unicode string of the value.\r
94b17fa1 2426 **/\r
2427CONST CHAR16*\r
2428EFIAPI\r
2429ShellCommandLineGetRawValue (\r
a405b86d 2430 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2431 IN UINTN Position\r
2432 )\r
2433{\r
94b17fa1 2434 LIST_ENTRY *Node;\r
2435\r
2436 //\r
2437 // check for CheckPackage == NULL\r
2438 //\r
2439 if (CheckPackage == NULL) {\r
2440 return (NULL);\r
2441 }\r
2442\r
2443 //\r
2444 // enumerate through the list of parametrs\r
2445 //\r
1e6e84c7 2446 for ( Node = GetFirstNode(CheckPackage)\r
2447 ; !IsNull (CheckPackage, Node)\r
2448 ; Node = GetNextNode(CheckPackage, Node)\r
a405b86d 2449 ){\r
94b17fa1 2450 //\r
2451 // If the position matches, return the value\r
2452 //\r
2453 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2454 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2455 }\r
2456 }\r
2457 return (NULL);\r
b1f95a06 2458}\r
2247dde4 2459\r
2460/**\r
1e6e84c7 2461 returns the number of command line value parameters that were parsed.\r
2462\r
2247dde4 2463 this will not include flags.\r
2464\r
a405b86d 2465 @param[in] CheckPackage The package of parsed command line arguments.\r
2466\r
2247dde4 2467 @retval (UINTN)-1 No parsing has ocurred\r
2468 @return other The number of value parameters found\r
2469**/\r
2470UINTN\r
2471EFIAPI\r
2472ShellCommandLineGetCount(\r
a405b86d 2473 IN CONST LIST_ENTRY *CheckPackage\r
125c2cf4 2474 )\r
2475{\r
a405b86d 2476 LIST_ENTRY *Node1;\r
2477 UINTN Count;\r
2478\r
2479 if (CheckPackage == NULL) {\r
2480 return (0);\r
2481 }\r
2482 for ( Node1 = GetFirstNode(CheckPackage), Count = 0\r
2483 ; !IsNull (CheckPackage, Node1)\r
2484 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2485 ){\r
2486 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {\r
2487 Count++;\r
2488 }\r
2489 }\r
2490 return (Count);\r
2247dde4 2491}\r
2492\r
36a9d672 2493/**\r
2494 Determins if a parameter is duplicated.\r
2495\r
1e6e84c7 2496 If Param is not NULL then it will point to a callee allocated string buffer\r
36a9d672 2497 with the parameter value if a duplicate is found.\r
2498\r
2499 If CheckPackage is NULL, then ASSERT.\r
2500\r
2501 @param[in] CheckPackage The package of parsed command line arguments.\r
2502 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2503\r
2504 @retval EFI_SUCCESS No parameters were duplicated.\r
2505 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2506 **/\r
2507EFI_STATUS\r
2508EFIAPI\r
2509ShellCommandLineCheckDuplicate (\r
2510 IN CONST LIST_ENTRY *CheckPackage,\r
2511 OUT CHAR16 **Param\r
2512 )\r
2513{\r
2514 LIST_ENTRY *Node1;\r
2515 LIST_ENTRY *Node2;\r
1e6e84c7 2516\r
36a9d672 2517 ASSERT(CheckPackage != NULL);\r
2518\r
1e6e84c7 2519 for ( Node1 = GetFirstNode(CheckPackage)\r
2520 ; !IsNull (CheckPackage, Node1)\r
2521 ; Node1 = GetNextNode(CheckPackage, Node1)\r
a405b86d 2522 ){\r
1e6e84c7 2523 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2524 ; !IsNull (CheckPackage, Node2)\r
2525 ; Node2 = GetNextNode(CheckPackage, Node2)\r
a405b86d 2526 ){\r
2527 if ((((SHELL_PARAM_PACKAGE*)Node1)->Name != NULL) && (((SHELL_PARAM_PACKAGE*)Node2)->Name != NULL) && StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r
36a9d672 2528 if (Param != NULL) {\r
2529 *Param = NULL;\r
2530 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2531 }\r
2532 return (EFI_DEVICE_ERROR);\r
2533 }\r
2534 }\r
2535 }\r
2536 return (EFI_SUCCESS);\r
2537}\r
2538\r
975136ab 2539/**\r
1e6e84c7 2540 This is a find and replace function. Upon successful return the NewString is a copy of\r
975136ab 2541 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2542\r
b3011f40 2543 If SourceString and NewString overlap the behavior is undefined.\r
2544\r
975136ab 2545 If the string would grow bigger than NewSize it will halt and return error.\r
2546\r
4ff7e37b
ED
2547 @param[in] SourceString The string with source buffer.\r
2548 @param[in, out] NewString The string with resultant buffer.\r
2549 @param[in] NewSize The size in bytes of NewString.\r
2550 @param[in] FindTarget The string to look for.\r
2551 @param[in] ReplaceWith The string to replace FindTarget with.\r
2552 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2553 immediately before it.\r
2554 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
969c783b 2555\r
2556 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2557 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2558 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2559 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2560 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2561 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
1e6e84c7 2562 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
969c783b 2563 the new string (truncation occurred).\r
a405b86d 2564 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
975136ab 2565**/\r
975136ab 2566EFI_STATUS\r
2567EFIAPI\r
a405b86d 2568ShellCopySearchAndReplace(\r
975136ab 2569 IN CHAR16 CONST *SourceString,\r
a405b86d 2570 IN OUT CHAR16 *NewString,\r
975136ab 2571 IN UINTN NewSize,\r
2572 IN CONST CHAR16 *FindTarget,\r
969c783b 2573 IN CONST CHAR16 *ReplaceWith,\r
a405b86d 2574 IN CONST BOOLEAN SkipPreCarrot,\r
2575 IN CONST BOOLEAN ParameterReplacing\r
1e6e84c7 2576 )\r
2247dde4 2577{\r
0158294b 2578 UINTN Size;\r
a405b86d 2579 CHAR16 *Replace;\r
2580\r
975136ab 2581 if ( (SourceString == NULL)\r
2582 || (NewString == NULL)\r
2583 || (FindTarget == NULL)\r
2584 || (ReplaceWith == NULL)\r
2585 || (StrLen(FindTarget) < 1)\r
2586 || (StrLen(SourceString) < 1)\r
a405b86d 2587 ){\r
975136ab 2588 return (EFI_INVALID_PARAMETER);\r
2589 }\r
a405b86d 2590 Replace = NULL;\r
2591 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {\r
2592 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
2593 } else {\r
2594 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
beab0fc5 2595 if (Replace != NULL) {\r
2596 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
2597 }\r
a405b86d 2598 }\r
3e082d58 2599 if (Replace == NULL) {\r
2600 return (EFI_OUT_OF_RESOURCES);\r
2601 }\r
98c16be5 2602 NewString = ZeroMem(NewString, NewSize);\r
2247dde4 2603 while (*SourceString != CHAR_NULL) {\r
969c783b 2604 //\r
a405b86d 2605 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
969c783b 2606 // dont have a carrot do a replace...\r
2607 //\r
1e6e84c7 2608 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
a405b86d 2609 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)\r
2610 ){\r
975136ab 2611 SourceString += StrLen(FindTarget);\r
0158294b 2612 Size = StrSize(NewString);\r
a405b86d 2613 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {\r
2614 FreePool(Replace);\r
975136ab 2615 return (EFI_BUFFER_TOO_SMALL);\r
2616 }\r
98c16be5 2617 StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString));\r
975136ab 2618 } else {\r
0158294b 2619 Size = StrSize(NewString);\r
2620 if (Size + sizeof(CHAR16) > NewSize) {\r
a405b86d 2621 FreePool(Replace);\r
975136ab 2622 return (EFI_BUFFER_TOO_SMALL);\r
2623 }\r
2624 StrnCat(NewString, SourceString, 1);\r
2625 SourceString++;\r
2626 }\r
2627 }\r
a405b86d 2628 FreePool(Replace);\r
975136ab 2629 return (EFI_SUCCESS);\r
2630}\r
b1f95a06 2631\r
e2f8297f 2632/**\r
2633 Internal worker function to output a string.\r
2634\r
2635 This function will output a string to the correct StdOut.\r
2636\r
2637 @param[in] String The string to print out.\r
2638\r
2639 @retval EFI_SUCCESS The operation was sucessful.\r
2640 @retval !EFI_SUCCESS The operation failed.\r
2641**/\r
2642EFI_STATUS\r
2643EFIAPI\r
2644InternalPrintTo (\r
2645 IN CONST CHAR16 *String\r
2646 )\r
2647{\r
2648 UINTN Size;\r
2649 Size = StrSize(String) - sizeof(CHAR16);\r
a405b86d 2650 if (Size == 0) {\r
2651 return (EFI_SUCCESS);\r
2652 }\r
366f81a0 2653 if (gEfiShellParametersProtocol != NULL) {\r
2654 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
e2f8297f 2655 }\r
2656 if (mEfiShellInterface != NULL) {\r
06c355b4 2657 if (mEfiShellInterface->RedirArgc == 0) { \r
49bd498d 2658 //\r
2659 // Divide in half for old shell. Must be string length not size.\r
06c355b4 2660 // \r
2661 Size /=2; // Divide in half only when no redirection.\r
2662 }\r
a405b86d 2663 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
e2f8297f 2664 }\r
2665 ASSERT(FALSE);\r
2666 return (EFI_UNSUPPORTED);\r
2667}\r
2668\r
b1f95a06 2669/**\r
2670 Print at a specific location on the screen.\r
2671\r
f1b87e7a 2672 This function will move the cursor to a given screen location and print the specified string\r
1e6e84c7 2673\r
2674 If -1 is specified for either the Row or Col the current screen location for BOTH\r
f1b87e7a 2675 will be used.\r
b1f95a06 2676\r
2677 if either Row or Col is out of range for the current console, then ASSERT\r
2678 if Format is NULL, then ASSERT\r
2679\r
1e6e84c7 2680 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
b1f95a06 2681 the following additional flags:\r
2682 %N - Set output attribute to normal\r
2683 %H - Set output attribute to highlight\r
2684 %E - Set output attribute to error\r
2685 %B - Set output attribute to blue color\r
2686 %V - Set output attribute to green color\r
2687\r
2688 Note: The background color is controlled by the shell command cls.\r
2689\r
b1f95a06 2690 @param[in] Col the column to print at\r
252d9457 2691 @param[in] Row the row to print at\r
b1f95a06 2692 @param[in] Format the format string\r
2247dde4 2693 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2694\r
a405b86d 2695 @return EFI_SUCCESS The operation was successful.\r
2696 @return EFI_DEVICE_ERROR The console device reported an error.\r
b1f95a06 2697**/\r
a405b86d 2698EFI_STATUS\r
b1f95a06 2699EFIAPI\r
2247dde4 2700InternalShellPrintWorker(\r
b1f95a06 2701 IN INT32 Col OPTIONAL,\r
2702 IN INT32 Row OPTIONAL,\r
2703 IN CONST CHAR16 *Format,\r
252d9457 2704 IN VA_LIST Marker\r
1e6e84c7 2705 )\r
2247dde4 2706{\r
b1f95a06 2707 EFI_STATUS Status;\r
975136ab 2708 CHAR16 *ResumeLocation;\r
2709 CHAR16 *FormatWalker;\r
a405b86d 2710 UINTN OriginalAttribute;\r
89e8537a 2711 CHAR16 *mPostReplaceFormat;\r
2712 CHAR16 *mPostReplaceFormat2;\r
2713\r
2714 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2715 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
a405b86d 2716\r
f8d3e689 2717 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
2718 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2719 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2720 return (EFI_OUT_OF_RESOURCES);\r
2721 }\r
2722\r
a405b86d 2723 Status = EFI_SUCCESS;\r
2724 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
1e6e84c7 2725\r
975136ab 2726 //\r
2727 // Back and forth each time fixing up 1 of our flags...\r
2728 //\r
a405b86d 2729 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
975136ab 2730 ASSERT_EFI_ERROR(Status);\r
a405b86d 2731 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
975136ab 2732 ASSERT_EFI_ERROR(Status);\r
a405b86d 2733 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
975136ab 2734 ASSERT_EFI_ERROR(Status);\r
a405b86d 2735 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
975136ab 2736 ASSERT_EFI_ERROR(Status);\r
a405b86d 2737 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
975136ab 2738 ASSERT_EFI_ERROR(Status);\r
2739\r
2740 //\r
2741 // Use the last buffer from replacing to print from...\r
2742 //\r
a405b86d 2743 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
b1f95a06 2744\r
2745 if (Col != -1 && Row != -1) {\r
b1f95a06 2746 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
975136ab 2747 }\r
2748\r
ecd3d59f 2749 FormatWalker = mPostReplaceFormat2;\r
2247dde4 2750 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2751 //\r
2752 // Find the next attribute change request\r
2753 //\r
2754 ResumeLocation = StrStr(FormatWalker, L"%");\r
2755 if (ResumeLocation != NULL) {\r
2247dde4 2756 *ResumeLocation = CHAR_NULL;\r
975136ab 2757 }\r
2758 //\r
2759 // print the current FormatWalker string\r
2760 //\r
a405b86d 2761 if (StrLen(FormatWalker)>0) {\r
2762 Status = InternalPrintTo(FormatWalker);\r
2763 if (EFI_ERROR(Status)) {\r
2764 break;\r
2765 }\r
2766 }\r
2767\r
975136ab 2768 //\r
2769 // update the attribute\r
2770 //\r
2771 if (ResumeLocation != NULL) {\r
5d46f17b 2772 if (*(ResumeLocation-1) == L'^') {\r
8bb7441e 2773 //\r
2774 // Move cursor back 1 position to overwrite the ^\r
2775 //\r
2776 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);\r
2777\r
5d46f17b 2778 //\r
2779 // Print a simple '%' symbol\r
2780 //\r
2781 Status = InternalPrintTo(L"%");\r
2782 ResumeLocation = ResumeLocation - 1;\r
2783 } else {\r
2784 switch (*(ResumeLocation+1)) {\r
2785 case (L'N'):\r
2786 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
a405b86d 2787 break;\r
5d46f17b 2788 case (L'E'):\r
2789 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2790 break;\r
2791 case (L'H'):\r
2792 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2793 break;\r
2794 case (L'B'):\r
2795 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2796 break;\r
2797 case (L'V'):\r
2798 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2799 break;\r
2800 default:\r
2801 //\r
2802 // Print a simple '%' symbol\r
2803 //\r
2804 Status = InternalPrintTo(L"%");\r
2805 if (EFI_ERROR(Status)) {\r
2806 break;\r
2807 }\r
2808 ResumeLocation = ResumeLocation - 1;\r
2809 break;\r
2810 }\r
975136ab 2811 }\r
2812 } else {\r
2813 //\r
2814 // reset to normal now...\r
2815 //\r
975136ab 2816 break;\r
2817 }\r
2818\r
2819 //\r
2820 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2821 //\r
2822 FormatWalker = ResumeLocation + 2;\r
2823 }\r
b1f95a06 2824\r
a405b86d 2825 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
89e8537a 2826\r
2827 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2828 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
a405b86d 2829 return (Status);\r
5f7431d0 2830}\r
2247dde4 2831\r
2832/**\r
2833 Print at a specific location on the screen.\r
2834\r
e2f8297f 2835 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2836\r
2837 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2247dde4 2838 will be used.\r
2839\r
e2f8297f 2840 If either Row or Col is out of range for the current console, then ASSERT.\r
2841 If Format is NULL, then ASSERT.\r
2247dde4 2842\r
1e6e84c7 2843 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2844 the following additional flags:\r
2845 %N - Set output attribute to normal\r
2846 %H - Set output attribute to highlight\r
2847 %E - Set output attribute to error\r
2848 %B - Set output attribute to blue color\r
2849 %V - Set output attribute to green color\r
2850\r
2851 Note: The background color is controlled by the shell command cls.\r
2852\r
2247dde4 2853 @param[in] Col the column to print at\r
a405b86d 2854 @param[in] Row the row to print at\r
2247dde4 2855 @param[in] Format the format string\r
a405b86d 2856 @param[in] ... The variable argument list.\r
2247dde4 2857\r
a405b86d 2858 @return EFI_SUCCESS The printing was successful.\r
2859 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2860**/\r
a405b86d 2861EFI_STATUS\r
2247dde4 2862EFIAPI\r
2863ShellPrintEx(\r
2864 IN INT32 Col OPTIONAL,\r
2865 IN INT32 Row OPTIONAL,\r
2866 IN CONST CHAR16 *Format,\r
2867 ...\r
1e6e84c7 2868 )\r
2247dde4 2869{\r
2870 VA_LIST Marker;\r
a405b86d 2871 EFI_STATUS RetVal;\r
3e082d58 2872 if (Format == NULL) {\r
2873 return (EFI_INVALID_PARAMETER);\r
2874 }\r
2247dde4 2875 VA_START (Marker, Format);\r
a405b86d 2876 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
e2f8297f 2877 VA_END(Marker);\r
a405b86d 2878 return(RetVal);\r
2247dde4 2879}\r
2880\r
2881/**\r
2882 Print at a specific location on the screen.\r
2883\r
e2f8297f 2884 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2885\r
2886 If -1 is specified for either the Row or Col the current screen location for BOTH\r
e2f8297f 2887 will be used.\r
2247dde4 2888\r
e2f8297f 2889 If either Row or Col is out of range for the current console, then ASSERT.\r
2890 If Format is NULL, then ASSERT.\r
2247dde4 2891\r
1e6e84c7 2892 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2893 the following additional flags:\r
1e6e84c7 2894 %N - Set output attribute to normal.\r
2895 %H - Set output attribute to highlight.\r
2896 %E - Set output attribute to error.\r
2897 %B - Set output attribute to blue color.\r
2898 %V - Set output attribute to green color.\r
2247dde4 2899\r
2900 Note: The background color is controlled by the shell command cls.\r
2901\r
1e6e84c7 2902 @param[in] Col The column to print at.\r
a405b86d 2903 @param[in] Row The row to print at.\r
1e6e84c7 2904 @param[in] Language The language of the string to retrieve. If this parameter\r
2905 is NULL, then the current platform language is used.\r
2906 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2907 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
a405b86d 2908 @param[in] ... The variable argument list.\r
2247dde4 2909\r
a405b86d 2910 @return EFI_SUCCESS The printing was successful.\r
2911 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2912**/\r
a405b86d 2913EFI_STATUS\r
2247dde4 2914EFIAPI\r
2915ShellPrintHiiEx(\r
2916 IN INT32 Col OPTIONAL,\r
2917 IN INT32 Row OPTIONAL,\r
1e6e84c7 2918 IN CONST CHAR8 *Language OPTIONAL,\r
2247dde4 2919 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2920 IN CONST EFI_HANDLE HiiFormatHandle,\r
2921 ...\r
2922 )\r
2923{\r
2924 VA_LIST Marker;\r
2925 CHAR16 *HiiFormatString;\r
a405b86d 2926 EFI_STATUS RetVal;\r
2247dde4 2927\r
2928 VA_START (Marker, HiiFormatHandle);\r
1e6e84c7 2929 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2247dde4 2930 ASSERT(HiiFormatString != NULL);\r
2931\r
2932 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2933\r
a405b86d 2934 SHELL_FREE_NON_NULL(HiiFormatString);\r
e2f8297f 2935 VA_END(Marker);\r
2247dde4 2936\r
2937 return (RetVal);\r
2938}\r
2939\r
2940/**\r
2941 Function to determine if a given filename represents a file or a directory.\r
2942\r
2943 @param[in] DirName Path to directory to test.\r
2944\r
c8c22591 2945 @retval EFI_SUCCESS The Path represents a directory\r
2946 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2947 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2948 @return The path failed to open\r
2247dde4 2949**/\r
2950EFI_STATUS\r
2951EFIAPI\r
2952ShellIsDirectory(\r
2953 IN CONST CHAR16 *DirName\r
2954 )\r
2955{\r
2956 EFI_STATUS Status;\r
a405b86d 2957 SHELL_FILE_HANDLE Handle;\r
3e082d58 2958 CHAR16 *TempLocation;\r
2959 CHAR16 *TempLocation2;\r
2247dde4 2960\r
ecd3d59f 2961 ASSERT(DirName != NULL);\r
2962\r
a405b86d 2963 Handle = NULL;\r
2964 TempLocation = NULL;\r
2247dde4 2965\r
2966 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2967 if (EFI_ERROR(Status)) {\r
a405b86d 2968 //\r
2969 // try good logic first.\r
2970 //\r
366f81a0 2971 if (gEfiShellProtocol != NULL) {\r
3e082d58 2972 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
c8c22591 2973 if (TempLocation == NULL) {\r
2974 ShellCloseFile(&Handle);\r
2975 return (EFI_OUT_OF_RESOURCES);\r
2976 }\r
3e082d58 2977 TempLocation2 = StrStr(TempLocation, L":");\r
2978 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
2979 *(TempLocation2+1) = CHAR_NULL;\r
a405b86d 2980 }\r
366f81a0 2981 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
a405b86d 2982 FreePool(TempLocation);\r
2983 return (EFI_SUCCESS);\r
2984 }\r
2985 FreePool(TempLocation);\r
2986 } else {\r
2987 //\r
2988 // probably a map name?!?!!?\r
2989 //\r
2990 TempLocation = StrStr(DirName, L"\\");\r
2991 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {\r
2992 return (EFI_SUCCESS);\r
2993 }\r
2994 }\r
2247dde4 2995 return (Status);\r
2996 }\r
2997\r
2998 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2999 ShellCloseFile(&Handle);\r
3000 return (EFI_SUCCESS);\r
3001 }\r
3002 ShellCloseFile(&Handle);\r
3003 return (EFI_NOT_FOUND);\r
3004}\r
3005\r
36a9d672 3006/**\r
3007 Function to determine if a given filename represents a file.\r
3008\r
3009 @param[in] Name Path to file to test.\r
3010\r
3011 @retval EFI_SUCCESS The Path represents a file.\r
3012 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3013 @retval other The path failed to open.\r
3014**/\r
3015EFI_STATUS\r
3016EFIAPI\r
3017ShellIsFile(\r
3018 IN CONST CHAR16 *Name\r
3019 )\r
3020{\r
3021 EFI_STATUS Status;\r
a405b86d 3022 SHELL_FILE_HANDLE Handle;\r
36a9d672 3023\r
ecd3d59f 3024 ASSERT(Name != NULL);\r
3025\r
36a9d672 3026 Handle = NULL;\r
3027\r
3028 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
3029 if (EFI_ERROR(Status)) {\r
3030 return (Status);\r
3031 }\r
3032\r
3033 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
3034 ShellCloseFile(&Handle);\r
3035 return (EFI_SUCCESS);\r
3036 }\r
3037 ShellCloseFile(&Handle);\r
3038 return (EFI_NOT_FOUND);\r
3039}\r
3040\r
b3011f40 3041/**\r
3042 Function to determine if a given filename represents a file.\r
3043\r
3044 This will search the CWD and then the Path.\r
3045\r
3046 If Name is NULL, then ASSERT.\r
3047\r
3048 @param[in] Name Path to file to test.\r
3049\r
3050 @retval EFI_SUCCESS The Path represents a file.\r
3051 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3052 @retval other The path failed to open.\r
3053**/\r
3054EFI_STATUS\r
3055EFIAPI\r
3056ShellIsFileInPath(\r
3057 IN CONST CHAR16 *Name\r
a405b86d 3058 )\r
3059{\r
b3011f40 3060 CHAR16 *NewName;\r
3061 EFI_STATUS Status;\r
3062\r
3063 if (!EFI_ERROR(ShellIsFile(Name))) {\r
a405b86d 3064 return (EFI_SUCCESS);\r
b3011f40 3065 }\r
3066\r
3067 NewName = ShellFindFilePath(Name);\r
3068 if (NewName == NULL) {\r
3069 return (EFI_NOT_FOUND);\r
3070 }\r
3071 Status = ShellIsFile(NewName);\r
3072 FreePool(NewName);\r
3073 return (Status);\r
3074}\r
252d9457 3075\r
74b0fb8c
JC
3076/**\r
3077 Function return the number converted from a hex representation of a number.\r
3078\r
3079 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid\r
3080 result. Use ShellConvertStringToUint64 instead.\r
3081\r
3082 @param[in] String String representation of a number.\r
3083\r
3084 @return The unsigned integer result of the conversion.\r
3085 @retval (UINTN)(-1) An error occured.\r
3086**/\r
3087UINTN\r
3088EFIAPI\r
3089ShellHexStrToUintn(\r
3090 IN CONST CHAR16 *String\r
3091 )\r
3092{\r
3093 UINT64 RetVal;\r
3094\r
3095 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {\r
3096 return ((UINTN)RetVal);\r
3097 }\r
3098 \r
3099 return ((UINTN)(-1));\r
3100}\r
3101\r
125c2cf4 3102/**\r
1e6e84c7 3103 Function to determine whether a string is decimal or hex representation of a number\r
d59fc244 3104 and return the number converted from the string. Spaces are always skipped.\r
125c2cf4 3105\r
3106 @param[in] String String representation of a number\r
3107\r
252d9457 3108 @return the number\r
3109 @retval (UINTN)(-1) An error ocurred.\r
125c2cf4 3110**/\r
3111UINTN\r
3112EFIAPI\r
3113ShellStrToUintn(\r
3114 IN CONST CHAR16 *String\r
3115 )\r
3116{\r
252d9457 3117 UINT64 RetVal;\r
3118 BOOLEAN Hex;\r
3119\r
3120 Hex = FALSE;\r
3121\r
3122 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {\r
3123 Hex = TRUE;\r
125c2cf4 3124 }\r
252d9457 3125\r
3126 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
3127 return ((UINTN)RetVal);\r
3128 }\r
3129 return ((UINTN)(-1));\r
125c2cf4 3130}\r
3131\r
3132/**\r
3133 Safely append with automatic string resizing given length of Destination and\r
3134 desired length of copy from Source.\r
3135\r
3136 append the first D characters of Source to the end of Destination, where D is\r
3137 the lesser of Count and the StrLen() of Source. If appending those D characters\r
3138 will fit within Destination (whose Size is given as CurrentSize) and\r
1e6e84c7 3139 still leave room for a NULL terminator, then those characters are appended,\r
3140 starting at the original terminating NULL of Destination, and a new terminating\r
3141 NULL is appended.\r
125c2cf4 3142\r
3143 If appending D characters onto Destination will result in a overflow of the size\r
3144 given in CurrentSize the string will be grown such that the copy can be performed\r
3145 and CurrentSize will be updated to the new size.\r
3146\r
3147 If Source is NULL, there is nothing to append, just return the current buffer in\r
3148 Destination.\r
3149\r
3150 if Destination is NULL, then ASSERT()\r
3151 if Destination's current length (including NULL terminator) is already more then\r
3152 CurrentSize, then ASSERT()\r
3153\r
4ff7e37b
ED
3154 @param[in, out] Destination The String to append onto\r
3155 @param[in, out] CurrentSize on call the number of bytes in Destination. On\r
125c2cf4 3156 return possibly the new size (still in bytes). if NULL\r
3157 then allocate whatever is needed.\r
3158 @param[in] Source The String to append from\r
3159 @param[in] Count Maximum number of characters to append. if 0 then\r
3160 all are appended.\r
3161\r
3162 @return Destination return the resultant string.\r
3163**/\r
3164CHAR16*\r
3165EFIAPI\r
3166StrnCatGrow (\r
3167 IN OUT CHAR16 **Destination,\r
3168 IN OUT UINTN *CurrentSize,\r
3169 IN CONST CHAR16 *Source,\r
3170 IN UINTN Count\r
3171 )\r
3172{\r
3173 UINTN DestinationStartSize;\r
3174 UINTN NewSize;\r
3175\r
3176 //\r
3177 // ASSERTs\r
3178 //\r
3179 ASSERT(Destination != NULL);\r
3180\r
3181 //\r
3182 // If there's nothing to do then just return Destination\r
3183 //\r
3184 if (Source == NULL) {\r
3185 return (*Destination);\r
3186 }\r
3187\r
3188 //\r
3189 // allow for un-initialized pointers, based on size being 0\r
3190 //\r
3191 if (CurrentSize != NULL && *CurrentSize == 0) {\r
3192 *Destination = NULL;\r
3193 }\r
3194\r
3195 //\r
3196 // allow for NULL pointers address as Destination\r
3197 //\r
3198 if (*Destination != NULL) {\r
3199 ASSERT(CurrentSize != 0);\r
3200 DestinationStartSize = StrSize(*Destination);\r
3201 ASSERT(DestinationStartSize <= *CurrentSize);\r
3202 } else {\r
3203 DestinationStartSize = 0;\r
3204// ASSERT(*CurrentSize == 0);\r
3205 }\r
3206\r
3207 //\r
3208 // Append all of Source?\r
3209 //\r
3210 if (Count == 0) {\r
3211 Count = StrLen(Source);\r
3212 }\r
3213\r
3214 //\r
3215 // Test and grow if required\r
3216 //\r
3217 if (CurrentSize != NULL) {\r
3218 NewSize = *CurrentSize;\r
f480fdc0
ED
3219 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {\r
3220 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
3221 NewSize += 2 * Count * sizeof(CHAR16);\r
3222 }\r
3223 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
3224 *CurrentSize = NewSize;\r
125c2cf4 3225 }\r
125c2cf4 3226 } else {\r
3227 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
3228 }\r
3229\r
3230 //\r
3231 // Now use standard StrnCat on a big enough buffer\r
3232 //\r
c9d92df0 3233 if (*Destination == NULL) {\r
3234 return (NULL);\r
3235 }\r
125c2cf4 3236 return StrnCat(*Destination, Source, Count);\r
3237}\r
c9d92df0 3238\r
3239/**\r
3240 Prompt the user and return the resultant answer to the requestor.\r
3241\r
3242 This function will display the requested question on the shell prompt and then\r
3243 wait for an apropriate answer to be input from the console.\r
3244\r
a405b86d 3245 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
c9d92df0 3246 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3247\r
a405b86d 3248 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
c9d92df0 3249 CHAR16*.\r
3250\r
3251 In either case *Response must be callee freed if Response was not NULL;\r
3252\r
3253 @param Type What type of question is asked. This is used to filter the input\r
3254 to prevent invalid answers to question.\r
3255 @param Prompt Pointer to string prompt to use to request input.\r
3256 @param Response Pointer to Response which will be populated upon return.\r
3257\r
3258 @retval EFI_SUCCESS The operation was sucessful.\r
3259 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3260 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3261 @return other The operation failed.\r
3262**/\r
3263EFI_STATUS\r
3264EFIAPI\r
3265ShellPromptForResponse (\r
3266 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3267 IN CHAR16 *Prompt OPTIONAL,\r
3268 IN OUT VOID **Response OPTIONAL\r
3269 )\r
3270{\r
3271 EFI_STATUS Status;\r
3272 EFI_INPUT_KEY Key;\r
3273 UINTN EventIndex;\r
3274 SHELL_PROMPT_RESPONSE *Resp;\r
a405b86d 3275 UINTN Size;\r
3276 CHAR16 *Buffer;\r
c9d92df0 3277\r
a405b86d 3278 Status = EFI_UNSUPPORTED;\r
3279 Resp = NULL;\r
3280 Buffer = NULL;\r
3281 Size = 0;\r
3282 if (Type != ShellPromptResponseTypeFreeform) {\r
252d9457 3283 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
3e082d58 3284 if (Resp == NULL) {\r
3285 return (EFI_OUT_OF_RESOURCES);\r
3286 }\r
90bfa227 3287 }\r
c9d92df0 3288\r
3289 switch(Type) {\r
a405b86d 3290 case ShellPromptResponseTypeQuitContinue:\r
c9d92df0 3291 if (Prompt != NULL) {\r
3292 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3293 }\r
3294 //\r
3295 // wait for valid response\r
3296 //\r
3297 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3298 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3299 if (EFI_ERROR(Status)) {\r
3300 break;\r
3301 }\r
c9d92df0 3302 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3303 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r
a405b86d 3304 *Resp = ShellPromptResponseQuit;\r
c9d92df0 3305 } else {\r
a405b86d 3306 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3307 }\r
3308 break;\r
a405b86d 3309 case ShellPromptResponseTypeYesNoCancel:\r
c9d92df0 3310 if (Prompt != NULL) {\r
3311 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3312 }\r
3313 //\r
3314 // wait for valid response\r
3315 //\r
a405b86d 3316 *Resp = ShellPromptResponseMax;\r
3317 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3318 if (ShellGetExecutionBreakFlag()) {\r
3319 Status = EFI_ABORTED;\r
3320 break;\r
3321 }\r
c9d92df0 3322 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3323 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3324 if (EFI_ERROR(Status)) {\r
3325 break;\r
3326 }\r
c9d92df0 3327 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3328 switch (Key.UnicodeChar) {\r
3329 case L'Y':\r
3330 case L'y':\r
a405b86d 3331 *Resp = ShellPromptResponseYes;\r
c9d92df0 3332 break;\r
3333 case L'N':\r
3334 case L'n':\r
a405b86d 3335 *Resp = ShellPromptResponseNo;\r
3336 break;\r
3337 case L'C':\r
3338 case L'c':\r
3339 *Resp = ShellPromptResponseCancel;\r
3340 break;\r
3341 }\r
3342 }\r
3343 break; case ShellPromptResponseTypeYesNoAllCancel:\r
3344 if (Prompt != NULL) {\r
3345 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3346 }\r
3347 //\r
3348 // wait for valid response\r
3349 //\r
3350 *Resp = ShellPromptResponseMax;\r
3351 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3352 if (ShellGetExecutionBreakFlag()) {\r
3353 Status = EFI_ABORTED;\r
3354 break;\r
3355 }\r
a405b86d 3356 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3357 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3358 if (EFI_ERROR(Status)) {\r
3359 break;\r
3360 }\r
a405b86d 3361 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3362 switch (Key.UnicodeChar) {\r
3363 case L'Y':\r
3364 case L'y':\r
3365 *Resp = ShellPromptResponseYes;\r
3366 break;\r
3367 case L'N':\r
3368 case L'n':\r
3369 *Resp = ShellPromptResponseNo;\r
c9d92df0 3370 break;\r
3371 case L'A':\r
3372 case L'a':\r
a405b86d 3373 *Resp = ShellPromptResponseAll;\r
c9d92df0 3374 break;\r
3375 case L'C':\r
3376 case L'c':\r
a405b86d 3377 *Resp = ShellPromptResponseCancel;\r
c9d92df0 3378 break;\r
3379 }\r
3380 }\r
3381 break;\r
a405b86d 3382 case ShellPromptResponseTypeEnterContinue:\r
3383 case ShellPromptResponseTypeAnyKeyContinue:\r
c9d92df0 3384 if (Prompt != NULL) {\r
3385 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3386 }\r
3387 //\r
3388 // wait for valid response\r
3389 //\r
a405b86d 3390 *Resp = ShellPromptResponseMax;\r
3391 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3392 if (ShellGetExecutionBreakFlag()) {\r
3393 Status = EFI_ABORTED;\r
3394 break;\r
3395 }\r
c9d92df0 3396 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
a405b86d 3397 if (Type == ShellPromptResponseTypeEnterContinue) {\r
c9d92df0 3398 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3399 if (EFI_ERROR(Status)) {\r
3400 break;\r
3401 }\r
c9d92df0 3402 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3403 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
a405b86d 3404 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3405 break;\r
3406 }\r
3407 }\r
a405b86d 3408 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3409 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3410 ASSERT_EFI_ERROR(Status);\r
3411 *Resp = ShellPromptResponseContinue;\r
3412 break;\r
3413 }\r
3414 }\r
3415 break;\r
3416 case ShellPromptResponseTypeYesNo:\r
3417 if (Prompt != NULL) {\r
3418 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3419 }\r
3420 //\r
3421 // wait for valid response\r
3422 //\r
3423 *Resp = ShellPromptResponseMax;\r
3424 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3425 if (ShellGetExecutionBreakFlag()) {\r
3426 Status = EFI_ABORTED;\r
3427 break;\r
3428 }\r
a405b86d 3429 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3430 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3431 if (EFI_ERROR(Status)) {\r
3432 break;\r
3433 }\r
a405b86d 3434 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3435 switch (Key.UnicodeChar) {\r
3436 case L'Y':\r
3437 case L'y':\r
3438 *Resp = ShellPromptResponseYes;\r
3439 break;\r
3440 case L'N':\r
3441 case L'n':\r
3442 *Resp = ShellPromptResponseNo;\r
3443 break;\r
3444 }\r
3445 }\r
3446 break;\r
3447 case ShellPromptResponseTypeFreeform:\r
3448 if (Prompt != NULL) {\r
3449 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3450 }\r
3451 while(1) {\r
194ae48d
JC
3452 if (ShellGetExecutionBreakFlag()) {\r
3453 Status = EFI_ABORTED;\r
3454 break;\r
3455 }\r
a405b86d 3456 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3457 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3458 if (EFI_ERROR(Status)) {\r
3459 break;\r
3460 }\r
a405b86d 3461 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3462 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
c9d92df0 3463 break;\r
3464 }\r
a405b86d 3465 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3466 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);\r
c9d92df0 3467 }\r
3468 break;\r
a405b86d 3469 //\r
3470 // This is the location to add new prompt types.\r
194ae48d 3471 // If your new type loops remember to add ExecutionBreak support.\r
a405b86d 3472 //\r
c9d92df0 3473 default:\r
a405b86d 3474 ASSERT(FALSE);\r
c9d92df0 3475 }\r
3476\r
3477 if (Response != NULL) {\r
a405b86d 3478 if (Resp != NULL) {\r
3479 *Response = Resp;\r
3480 } else if (Buffer != NULL) {\r
3481 *Response = Buffer;\r
3482 }\r
c9d92df0 3483 } else {\r
a405b86d 3484 if (Resp != NULL) {\r
3485 FreePool(Resp);\r
3486 }\r
3487 if (Buffer != NULL) {\r
3488 FreePool(Buffer);\r
3489 }\r
c9d92df0 3490 }\r
3491\r
a405b86d 3492 ShellPrintEx(-1, -1, L"\r\n");\r
c9d92df0 3493 return (Status);\r
3494}\r
3495\r
3496/**\r
3497 Prompt the user and return the resultant answer to the requestor.\r
3498\r
3499 This function is the same as ShellPromptForResponse, except that the prompt is\r
3500 automatically pulled from HII.\r
3501\r
3502 @param Type What type of question is asked. This is used to filter the input\r
3503 to prevent invalid answers to question.\r
a405b86d 3504 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3505 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3506 @param Response Pointer to Response which will be populated upon return.\r
c9d92df0 3507\r
3508 @retval EFI_SUCCESS the operation was sucessful.\r
3509 @return other the operation failed.\r
3510\r
3511 @sa ShellPromptForResponse\r
3512**/\r
3513EFI_STATUS\r
3514EFIAPI\r
3515ShellPromptForResponseHii (\r
3516 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3517 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3518 IN CONST EFI_HANDLE HiiFormatHandle,\r
3519 IN OUT VOID **Response\r
3520 )\r
3521{\r
3522 CHAR16 *Prompt;\r
3523 EFI_STATUS Status;\r
3524\r
3525 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
3526 Status = ShellPromptForResponse(Type, Prompt, Response);\r
3527 FreePool(Prompt);\r
3528 return (Status);\r
3529}\r
3530\r
a405b86d 3531/**\r
3532 Function to determin if an entire string is a valid number.\r
3533\r
3534 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
c9d92df0 3535\r
a405b86d 3536 @param[in] String The string to evaluate.\r
3537 @param[in] ForceHex TRUE - always assume hex.\r
3538 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3539\r
3540 @retval TRUE It is all numeric (dec/hex) characters.\r
3541 @retval FALSE There is a non-numeric character.\r
3542**/\r
3543BOOLEAN\r
3544EFIAPI\r
252d9457 3545InternalShellIsHexOrDecimalNumber (\r
a405b86d 3546 IN CONST CHAR16 *String,\r
3547 IN CONST BOOLEAN ForceHex,\r
3548 IN CONST BOOLEAN StopAtSpace\r
3549 )\r
3550{\r
3551 BOOLEAN Hex;\r
3552\r
3553 //\r
3554 // chop off a single negative sign\r
3555 //\r
3556 if (String != NULL && *String == L'-') {\r
3557 String++;\r
3558 }\r
b0934ac4 3559\r
a405b86d 3560 if (String == NULL) {\r
3561 return (FALSE);\r
3562 }\r
3563\r
3564 //\r
3565 // chop leading zeroes\r
3566 //\r
3567 while(String != NULL && *String == L'0'){\r
3568 String++;\r
3569 }\r
3570 //\r
3571 // allow '0x' or '0X', but not 'x' or 'X'\r
3572 //\r
3573 if (String != NULL && (*String == L'x' || *String == L'X')) {\r
3574 if (*(String-1) != L'0') {\r
3575 //\r
3576 // we got an x without a preceeding 0\r
3577 //\r
3578 return (FALSE);\r
3579 }\r
3580 String++;\r
3581 Hex = TRUE;\r
3582 } else if (ForceHex) {\r
3583 Hex = TRUE;\r
3584 } else {\r
3585 Hex = FALSE;\r
3586 }\r
3587\r
3588 //\r
3589 // loop through the remaining characters and use the lib function\r
3590 //\r
3591 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
3592 if (Hex) {\r
3593 if (!ShellIsHexaDecimalDigitCharacter(*String)) {\r
3594 return (FALSE);\r
3595 }\r
3596 } else {\r
3597 if (!ShellIsDecimalDigitCharacter(*String)) {\r
3598 return (FALSE);\r
3599 }\r
3600 }\r
3601 }\r
252d9457 3602\r
a405b86d 3603 return (TRUE);\r
3604}\r
3605\r
3606/**\r
3607 Function to determine if a given filename exists.\r
3608\r
3609 @param[in] Name Path to test.\r
3610\r
3611 @retval EFI_SUCCESS The Path represents a file.\r
3612 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3613 @retval other The path failed to open.\r
3614**/\r
3615EFI_STATUS\r
3616EFIAPI\r
3617ShellFileExists(\r
3618 IN CONST CHAR16 *Name\r
3619 )\r
3620{\r
3621 EFI_STATUS Status;\r
3622 EFI_SHELL_FILE_INFO *List;\r
3623\r
3624 ASSERT(Name != NULL);\r
3625\r
3626 List = NULL;\r
3627 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);\r
3628 if (EFI_ERROR(Status)) {\r
3629 return (Status);\r
3630 }\r
3631\r
3632 ShellCloseFileMetaArg(&List);\r
3633\r
3634 return (EFI_SUCCESS);\r
3635}\r
252d9457 3636\r
3637/**\r
b0934ac4 3638 Convert a Unicode character to upper case only if\r
252d9457 3639 it maps to a valid small-case ASCII character.\r
3640\r
3641 This internal function only deal with Unicode character\r
3642 which maps to a valid small-case ASCII character, i.e.\r
3643 L'a' to L'z'. For other Unicode character, the input character\r
3644 is returned directly.\r
3645\r
3646 @param Char The character to convert.\r
3647\r
3648 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
3649 @retval Unchanged Otherwise.\r
3650\r
3651**/\r
3652CHAR16\r
3653EFIAPI\r
3654InternalShellCharToUpper (\r
3655 IN CHAR16 Char\r
3656 )\r
3657{\r
3658 if (Char >= L'a' && Char <= L'z') {\r
3659 return (CHAR16) (Char - (L'a' - L'A'));\r
3660 }\r
3661\r
3662 return Char;\r
3663}\r
3664\r
3665/**\r
3666 Convert a Unicode character to numerical value.\r
3667\r
3668 This internal function only deal with Unicode character\r
3669 which maps to a valid hexadecimal ASII character, i.e.\r
b0934ac4 3670 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other\r
252d9457 3671 Unicode character, the value returned does not make sense.\r
3672\r
3673 @param Char The character to convert.\r
3674\r
3675 @return The numerical value converted.\r
3676\r
3677**/\r
3678UINTN\r
3679EFIAPI\r
3680InternalShellHexCharToUintn (\r
3681 IN CHAR16 Char\r
3682 )\r
3683{\r
3684 if (ShellIsDecimalDigitCharacter (Char)) {\r
3685 return Char - L'0';\r
3686 }\r
3687\r
3688 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
3689}\r
3690\r
3691/**\r
3692 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3693\r
3694 This function returns a value of type UINTN by interpreting the contents\r
3695 of the Unicode string specified by String as a hexadecimal number.\r
3696 The format of the input Unicode string String is:\r
3697\r
3698 [spaces][zeros][x][hexadecimal digits].\r
3699\r
3700 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3701 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3702 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3703 The function will ignore the pad space, which includes spaces or tab characters,\r
3704 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3705 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3706 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3707 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3708\r
3709 If String has only pad spaces, then zero is returned.\r
3710 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3711 then zero is returned.\r
3712\r
3713 @param[in] String A pointer to a Null-terminated Unicode string.\r
3714 @param[out] Value Upon a successful return the value of the conversion.\r
3715 @param[in] StopAtSpace FALSE to skip spaces.\r
3716\r
3717 @retval EFI_SUCCESS The conversion was successful.\r
3718 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3719 @retval EFI_DEVICE_ERROR An overflow occured.\r
3720**/\r
3721EFI_STATUS\r
3722EFIAPI\r
3723InternalShellStrHexToUint64 (\r
3724 IN CONST CHAR16 *String,\r
3725 OUT UINT64 *Value,\r
3726 IN CONST BOOLEAN StopAtSpace\r
3727 )\r
3728{\r
3729 UINT64 Result;\r
3730\r
3731 if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
3732 return (EFI_INVALID_PARAMETER);\r
3733 }\r
b0934ac4 3734\r
252d9457 3735 //\r
b0934ac4 3736 // Ignore the pad spaces (space or tab)\r
252d9457 3737 //\r
3738 while ((*String == L' ') || (*String == L'\t')) {\r
3739 String++;\r
3740 }\r
3741\r
3742 //\r
3743 // Ignore leading Zeros after the spaces\r
3744 //\r
3745 while (*String == L'0') {\r
3746 String++;\r
3747 }\r
3748\r
3749 if (InternalShellCharToUpper (*String) == L'X') {\r
3750 if (*(String - 1) != L'0') {\r
3751 return 0;\r
3752 }\r
3753 //\r
3754 // Skip the 'X'\r
3755 //\r
3756 String++;\r
3757 }\r
3758\r
3759 Result = 0;\r
b0934ac4 3760\r
252d9457 3761 //\r
12ea4694 3762 // Skip spaces if requested\r
252d9457 3763 //\r
12ea4694
ED
3764 while (StopAtSpace && *String == L' ') {\r
3765 String++;\r
252d9457 3766 }\r
b0934ac4 3767\r
252d9457 3768 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
3769 //\r
b0934ac4 3770 // If the Hex Number represented by String overflows according\r
252d9457 3771 // to the range defined by UINTN, then ASSERT().\r
3772 //\r
3773 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
3774// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
3775 return (EFI_DEVICE_ERROR);\r
3776 }\r
3777\r
3778 Result = (LShiftU64(Result, 4));\r
3779 Result += InternalShellHexCharToUintn (*String);\r
3780 String++;\r
3781\r
3782 //\r
49bd498d 3783 // stop at spaces if requested\r
252d9457 3784 //\r
49bd498d 3785 if (StopAtSpace && *String == L' ') {\r
3786 break;\r
252d9457 3787 }\r
3788 }\r
3789\r
3790 *Value = Result;\r
3791 return (EFI_SUCCESS);\r
3792}\r
3793\r
3794/**\r
3795 Convert a Null-terminated Unicode decimal string to a value of\r
3796 type UINT64.\r
3797\r
3798 This function returns a value of type UINT64 by interpreting the contents\r
3799 of the Unicode string specified by String as a decimal number. The format\r
3800 of the input Unicode string String is:\r
3801\r
3802 [spaces] [decimal digits].\r
3803\r
3804 The valid decimal digit character is in the range [0-9]. The\r
3805 function will ignore the pad space, which includes spaces or\r
3806 tab characters, before [decimal digits]. The running zero in the\r
3807 beginning of [decimal digits] will be ignored. Then, the function\r
3808 stops at the first character that is a not a valid decimal character\r
3809 or a Null-terminator, whichever one comes first.\r
3810\r
3811 If String has only pad spaces, then 0 is returned.\r
3812 If String has no pad spaces or valid decimal digits,\r
3813 then 0 is returned.\r
3814\r
3815 @param[in] String A pointer to a Null-terminated Unicode string.\r
3816 @param[out] Value Upon a successful return the value of the conversion.\r
3817 @param[in] StopAtSpace FALSE to skip spaces.\r
3818\r
3819 @retval EFI_SUCCESS The conversion was successful.\r
3820 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3821 @retval EFI_DEVICE_ERROR An overflow occured.\r
3822**/\r
3823EFI_STATUS\r
3824EFIAPI\r
3825InternalShellStrDecimalToUint64 (\r
3826 IN CONST CHAR16 *String,\r
3827 OUT UINT64 *Value,\r
3828 IN CONST BOOLEAN StopAtSpace\r
3829 )\r
3830{\r
3831 UINT64 Result;\r
3832\r
3833 if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
3834 return (EFI_INVALID_PARAMETER);\r
3835 }\r
3836\r
3837 //\r
3838 // Ignore the pad spaces (space or tab)\r
3839 //\r
3840 while ((*String == L' ') || (*String == L'\t')) {\r
3841 String++;\r
3842 }\r
3843\r
3844 //\r
3845 // Ignore leading Zeros after the spaces\r
3846 //\r
3847 while (*String == L'0') {\r
3848 String++;\r
3849 }\r
3850\r
3851 Result = 0;\r
3852\r
3853 //\r
3854 // Skip spaces if requested\r
3855 //\r
3856 while (StopAtSpace && *String == L' ') {\r
3857 String++;\r
3858 }\r
3859 while (ShellIsDecimalDigitCharacter (*String)) {\r
3860 //\r
b0934ac4 3861 // If the number represented by String overflows according\r
252d9457 3862 // to the range defined by UINT64, then ASSERT().\r
3863 //\r
b0934ac4 3864\r
252d9457 3865 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
3866 return (EFI_DEVICE_ERROR);\r
3867 }\r
3868\r
3869 Result = MultU64x32(Result, 10) + (*String - L'0');\r
3870 String++;\r
3871\r
3872 //\r
3873 // Stop at spaces if requested\r
3874 //\r
3875 if (StopAtSpace && *String == L' ') {\r
3876 break;\r
3877 }\r
3878 }\r
3879\r
3880 *Value = Result;\r
b0934ac4 3881\r
252d9457 3882 return (EFI_SUCCESS);\r
3883}\r
3884\r
3885/**\r
3886 Function to verify and convert a string to its numerical value.\r
3887\r
3888 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
3889\r
3890 @param[in] String The string to evaluate.\r
3891 @param[out] Value Upon a successful return the value of the conversion.\r
3892 @param[in] ForceHex TRUE - always assume hex.\r
3893 @param[in] StopAtSpace FALSE to skip spaces.\r
b0934ac4 3894\r
252d9457 3895 @retval EFI_SUCCESS The conversion was successful.\r
3896 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
3897 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
3898**/\r
3899EFI_STATUS\r
3900EFIAPI\r
3901ShellConvertStringToUint64(\r
3902 IN CONST CHAR16 *String,\r
3903 OUT UINT64 *Value,\r
3904 IN CONST BOOLEAN ForceHex,\r
3905 IN CONST BOOLEAN StopAtSpace\r
3906 )\r
3907{\r
3908 UINT64 RetVal;\r
3909 CONST CHAR16 *Walker;\r
3910 EFI_STATUS Status;\r
3911 BOOLEAN Hex;\r
3912\r
3913 Hex = ForceHex;\r
3914\r
3915 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3916 if (!Hex) {\r
3917 Hex = TRUE;\r
3918 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3919 return (EFI_INVALID_PARAMETER);\r
3920 }\r
3921 } else {\r
3922 return (EFI_INVALID_PARAMETER);\r
3923 }\r
3924 }\r
3925\r
3926 //\r
3927 // Chop off leading spaces\r
3928 //\r
3929 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
3930\r
3931 //\r
3932 // make sure we have something left that is numeric.\r
3933 //\r
3934 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {\r
3935 return (EFI_INVALID_PARAMETER);\r
b0934ac4 3936 }\r
252d9457 3937\r
3938 //\r
3939 // do the conversion.\r
3940 //\r
3941 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
3942 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
3943 } else {\r
3944 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
3945 }\r
3946\r
3947 if (Value == NULL && !EFI_ERROR(Status)) {\r
3948 return (EFI_NOT_FOUND);\r
3949 }\r
3950\r
3951 if (Value != NULL) {\r
3952 *Value = RetVal;\r
3953 }\r
3954\r
3955 return (Status);\r
3956}\r
3957\r
3958/**\r
3959 Function to determin if an entire string is a valid number.\r
3960\r
3961 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3962\r
3963 @param[in] String The string to evaluate.\r
3964 @param[in] ForceHex TRUE - always assume hex.\r
3965 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3966\r
3967 @retval TRUE It is all numeric (dec/hex) characters.\r
3968 @retval FALSE There is a non-numeric character.\r
3969**/\r
3970BOOLEAN\r
3971EFIAPI\r
3972ShellIsHexOrDecimalNumber (\r
3973 IN CONST CHAR16 *String,\r
3974 IN CONST BOOLEAN ForceHex,\r
3975 IN CONST BOOLEAN StopAtSpace\r
3976 )\r
3977{\r
3978 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
3979 return (TRUE);\r
3980 }\r
3981 return (FALSE);\r
3982}\r
4d0a4fce 3983\r
3984/**\r
3985 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
3986 buffer. The returned buffer must be callee freed.\r
3987\r
3988 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
3989 maintained and not changed for all operations with the same file.\r
3990\r
4ff7e37b
ED
3991 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
3992 @param[in, out] Ascii Boolean value for indicating whether the file is\r
3993 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 3994\r
beab0fc5 3995 @return The line of text from the file.\r
3996 @retval NULL There was not enough memory available.\r
4d0a4fce 3997\r
3998 @sa ShellFileHandleReadLine\r
3999**/\r
4000CHAR16*\r
4001EFIAPI\r
4002ShellFileHandleReturnLine(\r
4003 IN SHELL_FILE_HANDLE Handle,\r
4004 IN OUT BOOLEAN *Ascii\r
4005 )\r
4006{\r
4007 CHAR16 *RetVal;\r
4008 UINTN Size;\r
4009 EFI_STATUS Status;\r
4010\r
4011 Size = 0;\r
4012 RetVal = NULL;\r
4013\r
4014 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
4015 if (Status == EFI_BUFFER_TOO_SMALL) {\r
4016 RetVal = AllocateZeroPool(Size);\r
beab0fc5 4017 if (RetVal == NULL) {\r
4018 return (NULL);\r
4019 }\r
4d0a4fce 4020 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
b0934ac4 4021\r
4d0a4fce 4022 }\r
4d0a4fce 4023 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
4024 FreePool(RetVal);\r
4025 RetVal = NULL;\r
4026 }\r
4027 return (RetVal);\r
4028}\r
4029\r
4030/**\r
4031 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
4032\r
4033 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
4034 maintained and not changed for all operations with the same file.\r
4035\r
4ff7e37b
ED
4036 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
4037 @param[in, out] Buffer The pointer to buffer to read into.\r
4038 @param[in, out] Size The pointer to number of bytes in Buffer.\r
4039 @param[in] Truncate If the buffer is large enough, this has no effect.\r
4040 If the buffer is is too small and Truncate is TRUE,\r
4041 the line will be truncated.\r
4042 If the buffer is is too small and Truncate is FALSE,\r
4043 then no read will occur.\r
4044\r
4045 @param[in, out] Ascii Boolean value for indicating whether the file is\r
4046 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 4047\r
4048 @retval EFI_SUCCESS The operation was successful. The line is stored in\r
4049 Buffer.\r
4050 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
4051 @retval EFI_INVALID_PARAMETER Size was NULL.\r
4052 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
4053 Size was updated to the minimum space required.\r
4054**/\r
4055EFI_STATUS\r
4056EFIAPI\r
4057ShellFileHandleReadLine(\r
4058 IN SHELL_FILE_HANDLE Handle,\r
4059 IN OUT CHAR16 *Buffer,\r
4060 IN OUT UINTN *Size,\r
4061 IN BOOLEAN Truncate,\r
4062 IN OUT BOOLEAN *Ascii\r
4063 )\r
4064{\r
4065 EFI_STATUS Status;\r
4066 CHAR16 CharBuffer;\r
4067 UINTN CharSize;\r
4068 UINTN CountSoFar;\r
4069 UINT64 OriginalFilePosition;\r
4070\r
4071\r
4072 if (Handle == NULL\r
4073 ||Size == NULL\r
4074 ){\r
4075 return (EFI_INVALID_PARAMETER);\r
4076 }\r
4077 if (Buffer == NULL) {\r
4078 ASSERT(*Size == 0);\r
4079 } else {\r
4080 *Buffer = CHAR_NULL;\r
4081 }\r
4082 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
4083 if (OriginalFilePosition == 0) {\r
4084 CharSize = sizeof(CHAR16);\r
4085 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
4086 ASSERT_EFI_ERROR(Status);\r
4087 if (CharBuffer == gUnicodeFileTag) {\r
4088 *Ascii = FALSE;\r
4089 } else {\r
4090 *Ascii = TRUE;\r
4091 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
4092 }\r
4093 }\r
4094\r
4095 for (CountSoFar = 0;;CountSoFar++){\r
4096 CharBuffer = 0;\r
4097 if (*Ascii) {\r
4098 CharSize = sizeof(CHAR8);\r
4099 } else {\r
4100 CharSize = sizeof(CHAR16);\r
4101 }\r
4102 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
4103 if ( EFI_ERROR(Status)\r
4104 || CharSize == 0\r
4105 || (CharBuffer == L'\n' && !(*Ascii))\r
4106 || (CharBuffer == '\n' && *Ascii)\r
4107 ){\r
4108 break;\r
4109 }\r
4110 //\r
4111 // if we have space save it...\r
4112 //\r
4113 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
4114 ASSERT(Buffer != NULL);\r
4115 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
4116 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
4117 }\r
4118 }\r
4119\r
4120 //\r
4121 // if we ran out of space tell when...\r
4122 //\r
4123 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
4124 *Size = (CountSoFar+1)*sizeof(CHAR16);\r
4125 if (!Truncate) {\r
4126 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
4127 } else {\r
4128 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
4129 }\r
4130 return (EFI_BUFFER_TOO_SMALL);\r
4131 }\r
4132 while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
4133 Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
4134 }\r
4135\r
4136 return (Status);\r
4137}\r
fb5278ef 4138\r
365aa98a 4139/**\r
4140 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.\r
4141\r
4142 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.\r
4143 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).\r
4144 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints \r
4145 the help content only.\r
4146 @retval EFI_DEVICE_ERROR The help data format was incorrect.\r
4147 @retval EFI_NOT_FOUND The help data could not be found.\r
4148 @retval EFI_SUCCESS The operation was successful.\r
4149**/\r
4150EFI_STATUS\r
4151EFIAPI\r
4152ShellPrintHelp (\r
4153 IN CONST CHAR16 *CommandToGetHelpOn,\r
4154 IN CONST CHAR16 *SectionToGetHelpOn,\r
4155 IN BOOLEAN PrintCommandText\r
4156 )\r
4157{\r
4158 EFI_STATUS Status;\r
4159 CHAR16 *OutText;\r
4160 \r
4161 OutText = NULL;\r
4162 \r
4163 //\r
4164 // Get the string to print based\r
4165 //\r
4166 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);\r
4167 \r
4168 //\r
4169 // make sure we got a valid string\r
4170 //\r
4171 if (EFI_ERROR(Status)){\r
4172 return Status;\r
4173 } \r
4174 if (OutText == NULL || StrLen(OutText) == 0) {\r
4175 return EFI_NOT_FOUND; \r
4176 }\r
4177 \r
4178 //\r
4179 // Chop off trailing stuff we dont need\r
4180 //\r
4181 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {\r
4182 OutText[StrLen(OutText)-1] = CHAR_NULL;\r
4183 }\r
4184 \r
4185 //\r
4186 // Print this out to the console\r
4187 //\r
4188 if (PrintCommandText) {\r
4189 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);\r
4190 } else {\r
4191 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);\r
4192 }\r
4193 \r
4194 SHELL_FREE_NON_NULL(OutText);\r
4195\r
4196 return EFI_SUCCESS;\r
4197}\r
4198\r
fb5278ef 4199/**\r
4200 Function to delete a file by name\r
4201 \r
4202 @param[in] FileName Pointer to file name to delete.\r
4203 \r
4204 @retval EFI_SUCCESS the file was deleted sucessfully\r
4205 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
4206 deleted\r
4207 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
4208 @retval EFI_NOT_FOUND The specified file could not be found on the\r
4209 device or the file system could not be found\r
4210 on the device.\r
4211 @retval EFI_NO_MEDIA The device has no medium.\r
4212 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
4213 medium is no longer supported.\r
4214 @retval EFI_DEVICE_ERROR The device reported an error.\r
4215 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
4216 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
4217 @retval EFI_ACCESS_DENIED The file was opened read only.\r
4218 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
4219 file.\r
4220 @retval other The file failed to open\r
4221**/\r
4222EFI_STATUS\r
4223EFIAPI\r
4224ShellDeleteFileByName(\r
4225 IN CONST CHAR16 *FileName\r
4226 )\r
4227{\r
4228 EFI_STATUS Status;\r
4229 SHELL_FILE_HANDLE FileHandle;\r
4230 \r
4231 Status = ShellFileExists(FileName);\r
4232 \r
4233 if (Status == EFI_SUCCESS){\r
4234 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);\r
4235 if (Status == EFI_SUCCESS){\r
4236 Status = ShellDeleteFile(&FileHandle);\r
4237 }\r
4238 } \r
4239\r
4240 return(Status);\r
4241 \r
4242}\r