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