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