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