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