]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
ShellPkg/IfConfig: Handle memory allocation failure
[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
2efafabf 2024 CHAR16 *NewValue;\r
94b17fa1 2025\r
2026 CurrentItemPackage = NULL;\r
125c2cf4 2027 GetItemValue = 0;\r
2028 ValueSize = 0;\r
a405b86d 2029 Count = 0;\r
94b17fa1 2030\r
2031 //\r
2032 // If there is only 1 item we dont need to do anything\r
2033 //\r
a405b86d 2034 if (Argc < 1) {\r
94b17fa1 2035 *CheckPackage = NULL;\r
2036 return (EFI_SUCCESS);\r
2037 }\r
2038\r
2247dde4 2039 //\r
2040 // ASSERTs\r
2041 //\r
2042 ASSERT(CheckList != NULL);\r
2043 ASSERT(Argv != NULL);\r
2044\r
94b17fa1 2045 //\r
2046 // initialize the linked list\r
2047 //\r
2048 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
02a758cb 2049 if (*CheckPackage == NULL) {\r
beab0fc5 2050 return (EFI_OUT_OF_RESOURCES);\r
02a758cb 2051 }\r
beab0fc5 2052\r
94b17fa1 2053 InitializeListHead(*CheckPackage);\r
2054\r
2055 //\r
2056 // loop through each of the arguments\r
2057 //\r
2058 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
2059 if (Argv[LoopCounter] == NULL) {\r
2060 //\r
2061 // do nothing for NULL argv\r
2062 //\r
a405b86d 2063 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
2247dde4 2064 //\r
2065 // We might have leftover if last parameter didnt have optional value\r
2066 //\r
125c2cf4 2067 if (GetItemValue != 0) {\r
2068 GetItemValue = 0;\r
2247dde4 2069 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2070 }\r
94b17fa1 2071 //\r
2072 // this is a flag\r
2073 //\r
252d9457 2074 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 2075 if (CurrentItemPackage == NULL) {\r
2076 ShellCommandLineFreeVarList(*CheckPackage);\r
2077 *CheckPackage = NULL;\r
2078 return (EFI_OUT_OF_RESOURCES);\r
2079 }\r
98c16be5 2080 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);\r
beab0fc5 2081 if (CurrentItemPackage->Name == NULL) {\r
2082 ShellCommandLineFreeVarList(*CheckPackage);\r
2083 *CheckPackage = NULL;\r
2084 return (EFI_OUT_OF_RESOURCES);\r
2085 }\r
94b17fa1 2086 CurrentItemPackage->Type = CurrentItemType;\r
2087 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 2088 CurrentItemPackage->Value = NULL;\r
94b17fa1 2089\r
2090 //\r
2091 // Does this flag require a value\r
2092 //\r
125c2cf4 2093 switch (CurrentItemPackage->Type) {\r
94b17fa1 2094 //\r
125c2cf4 2095 // possibly trigger the next loop(s) to populate the value of this item\r
1e6e84c7 2096 //\r
125c2cf4 2097 case TypeValue:\r
658bf43e 2098 case TypeTimeValue:\r
1e6e84c7 2099 GetItemValue = 1;\r
125c2cf4 2100 ValueSize = 0;\r
2101 break;\r
2102 case TypeDoubleValue:\r
2103 GetItemValue = 2;\r
2104 ValueSize = 0;\r
2105 break;\r
2106 case TypeMaxValue:\r
2107 GetItemValue = (UINTN)(-1);\r
2108 ValueSize = 0;\r
2109 break;\r
2110 default:\r
2111 //\r
2112 // this item has no value expected; we are done\r
2113 //\r
2114 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2115 ASSERT(GetItemValue == 0);\r
2116 break;\r
94b17fa1 2117 }\r
af7a3a54 2118 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {\r
b1f95a06 2119 //\r
125c2cf4 2120 // get the item VALUE for a previous flag\r
b1f95a06 2121 //\r
484dd08c 2122 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
2efafabf
RN
2123 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
2124 if (NewValue == NULL) {\r
2125 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);\r
2126 SHELL_FREE_NON_NULL (CurrentItemPackage);\r
2127 ShellCommandLineFreeVarList (*CheckPackage);\r
2128 *CheckPackage = NULL;\r
2129 return EFI_OUT_OF_RESOURCES;\r
2130 }\r
2131 CurrentItemPackage->Value = NewValue;\r
484dd08c 2132 if (ValueSize == 0) {\r
e75390f0
QS
2133 StrCpyS( CurrentItemPackage->Value, \r
2134 CurrentValueSize/sizeof(CHAR16), \r
2135 Argv[LoopCounter]\r
2136 );\r
125c2cf4 2137 } else {\r
e75390f0
QS
2138 StrCatS( CurrentItemPackage->Value, \r
2139 CurrentValueSize/sizeof(CHAR16), \r
2140 L" "\r
2141 );\r
2142 StrCatS( CurrentItemPackage->Value, \r
2143 CurrentValueSize/sizeof(CHAR16), \r
2144 Argv[LoopCounter]\r
2145 );\r
125c2cf4 2146 }\r
484dd08c
QS
2147 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
2148 \r
125c2cf4 2149 GetItemValue--;\r
2150 if (GetItemValue == 0) {\r
2151 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2152 }\r
658bf43e 2153 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){\r
b1f95a06 2154 //\r
2155 // add this one as a non-flag\r
2156 //\r
252d9457 2157\r
2158 TempPointer = Argv[LoopCounter];\r
b0934ac4 2159 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')\r
252d9457 2160 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
2161 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
2162 ){\r
2163 TempPointer++;\r
2164 }\r
2165 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 2166 if (CurrentItemPackage == NULL) {\r
2167 ShellCommandLineFreeVarList(*CheckPackage);\r
2168 *CheckPackage = NULL;\r
2169 return (EFI_OUT_OF_RESOURCES);\r
2170 }\r
b1f95a06 2171 CurrentItemPackage->Name = NULL;\r
2172 CurrentItemPackage->Type = TypePosition;\r
98c16be5 2173 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);\r
beab0fc5 2174 if (CurrentItemPackage->Value == NULL) {\r
2175 ShellCommandLineFreeVarList(*CheckPackage);\r
2176 *CheckPackage = NULL;\r
2177 return (EFI_OUT_OF_RESOURCES);\r
2178 }\r
a405b86d 2179 CurrentItemPackage->OriginalPosition = Count++;\r
9b3bf083 2180 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
252d9457 2181 } else {\r
94b17fa1 2182 //\r
2183 // this was a non-recognised flag... error!\r
2184 //\r
252d9457 2185 if (ProblemParam != NULL) {\r
98c16be5 2186 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);\r
252d9457 2187 }\r
94b17fa1 2188 ShellCommandLineFreeVarList(*CheckPackage);\r
2189 *CheckPackage = NULL;\r
2190 return (EFI_VOLUME_CORRUPTED);\r
94b17fa1 2191 }\r
2192 }\r
125c2cf4 2193 if (GetItemValue != 0) {\r
2194 GetItemValue = 0;\r
2195 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2196 }\r
94b17fa1 2197 //\r
2198 // support for AutoPageBreak\r
2199 //\r
2200 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
2201 ShellSetPageBreakMode(TRUE);\r
2202 }\r
2203 return (EFI_SUCCESS);\r
2204}\r
2205\r
2206/**\r
1e6e84c7 2207 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 2208 Optionally removes NULL values first.\r
1e6e84c7 2209\r
94b17fa1 2210 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 2211\r
a405b86d 2212 @param[in] CheckList The pointer to list of parameters to check.\r
2213 @param[out] CheckPackage The package of checked values.\r
2214 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
94b17fa1 2215 the paramater that caused failure.\r
a405b86d 2216 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2217 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
94b17fa1 2218\r
2219 @retval EFI_SUCCESS The operation completed sucessfully.\r
a405b86d 2220 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2221 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2222 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2223 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
1e6e84c7 2224 of the command line arguments was returned in\r
94b17fa1 2225 ProblemParam if provided.\r
a405b86d 2226 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2227 The invalid command line argument was returned in\r
94b17fa1 2228 ProblemParam if provided.\r
2229**/\r
2230EFI_STATUS\r
2231EFIAPI\r
2247dde4 2232ShellCommandLineParseEx (\r
94b17fa1 2233 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2234 OUT LIST_ENTRY **CheckPackage,\r
2235 OUT CHAR16 **ProblemParam OPTIONAL,\r
2247dde4 2236 IN BOOLEAN AutoPageBreak,\r
2237 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 2238 )\r
2239{\r
1e6e84c7 2240 //\r
94b17fa1 2241 // ASSERT that CheckList and CheckPackage aren't NULL\r
2242 //\r
2243 ASSERT(CheckList != NULL);\r
2244 ASSERT(CheckPackage != NULL);\r
2245\r
1e6e84c7 2246 //\r
94b17fa1 2247 // Check for UEFI Shell 2.0 protocols\r
2248 //\r
366f81a0 2249 if (gEfiShellParametersProtocol != NULL) {\r
1e6e84c7 2250 return (InternalCommandLineParse(CheckList,\r
2251 CheckPackage,\r
2252 ProblemParam,\r
2253 AutoPageBreak,\r
366f81a0 2254 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,\r
2255 gEfiShellParametersProtocol->Argc,\r
2247dde4 2256 AlwaysAllowNumbers));\r
94b17fa1 2257 }\r
2258\r
1e6e84c7 2259 //\r
94b17fa1 2260 // ASSERT That EFI Shell is not required\r
2261 //\r
2262 ASSERT (mEfiShellInterface != NULL);\r
1e6e84c7 2263 return (InternalCommandLineParse(CheckList,\r
2264 CheckPackage,\r
2265 ProblemParam,\r
2266 AutoPageBreak,\r
08d7f8e8 2267 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2247dde4 2268 mEfiShellInterface->Argc,\r
2269 AlwaysAllowNumbers));\r
94b17fa1 2270}\r
2271\r
2272/**\r
2273 Frees shell variable list that was returned from ShellCommandLineParse.\r
2274\r
2275 This function will free all the memory that was used for the CheckPackage\r
2276 list of postprocessed shell arguments.\r
2277\r
2278 this function has no return value.\r
2279\r
2280 if CheckPackage is NULL, then return\r
2281\r
2282 @param CheckPackage the list to de-allocate\r
2283 **/\r
2284VOID\r
2285EFIAPI\r
2286ShellCommandLineFreeVarList (\r
2287 IN LIST_ENTRY *CheckPackage\r
a405b86d 2288 )\r
2289{\r
94b17fa1 2290 LIST_ENTRY *Node;\r
2291\r
2292 //\r
2293 // check for CheckPackage == NULL\r
2294 //\r
2295 if (CheckPackage == NULL) {\r
2296 return;\r
2297 }\r
2298\r
2299 //\r
2300 // for each node in the list\r
2301 //\r
9eb53ac3 2302 for ( Node = GetFirstNode(CheckPackage)\r
a405b86d 2303 ; !IsListEmpty(CheckPackage)\r
9eb53ac3 2304 ; Node = GetFirstNode(CheckPackage)\r
a405b86d 2305 ){\r
94b17fa1 2306 //\r
2307 // Remove it from the list\r
2308 //\r
2309 RemoveEntryList(Node);\r
2310\r
2311 //\r
2312 // if it has a name free the name\r
2313 //\r
2314 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2315 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2316 }\r
2317\r
2318 //\r
2319 // if it has a value free the value\r
2320 //\r
2321 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2322 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2323 }\r
1e6e84c7 2324\r
94b17fa1 2325 //\r
2326 // free the node structure\r
2327 //\r
2328 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2329 }\r
2330 //\r
2331 // free the list head node\r
2332 //\r
2333 FreePool(CheckPackage);\r
2334}\r
2335/**\r
2336 Checks for presence of a flag parameter\r
2337\r
2338 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2339\r
2340 if CheckPackage is NULL then return FALSE.\r
2341 if KeyString is NULL then ASSERT()\r
1e6e84c7 2342\r
94b17fa1 2343 @param CheckPackage The package of parsed command line arguments\r
2344 @param KeyString the Key of the command line argument to check for\r
2345\r
2346 @retval TRUE the flag is on the command line\r
2347 @retval FALSE the flag is not on the command line\r
2348 **/\r
2349BOOLEAN\r
2350EFIAPI\r
2351ShellCommandLineGetFlag (\r
a405b86d 2352 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2353 IN CONST CHAR16 * CONST KeyString\r
2354 )\r
2355{\r
94b17fa1 2356 LIST_ENTRY *Node;\r
252d9457 2357 CHAR16 *TempString;\r
94b17fa1 2358\r
2359 //\r
0c1950ba 2360 // return FALSE for no package or KeyString is NULL\r
94b17fa1 2361 //\r
0c1950ba 2362 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2363 return (FALSE);\r
2364 }\r
2365\r
2366 //\r
2367 // enumerate through the list of parametrs\r
2368 //\r
1e6e84c7 2369 for ( Node = GetFirstNode(CheckPackage)\r
2370 ; !IsNull (CheckPackage, Node)\r
2371 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2372 ){\r
94b17fa1 2373 //\r
2374 // If the Name matches, return TRUE (and there may be NULL name)\r
2375 //\r
2376 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2377 //\r
2378 // If Type is TypeStart then only compare the begining of the strings\r
2379 //\r
252d9457 2380 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2381 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2382 return (TRUE);\r
2383 }\r
2384 TempString = NULL;\r
2385 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2386 if (TempString != NULL) {\r
2387 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2388 FreePool(TempString);\r
2389 return (TRUE);\r
2390 }\r
2391 FreePool(TempString);\r
2392 }\r
2393 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2394 return (TRUE);\r
2395 }\r
2396 }\r
2397 }\r
2398 return (FALSE);\r
2399}\r
2400/**\r
a405b86d 2401 Returns value from command line argument.\r
94b17fa1 2402\r
a405b86d 2403 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
1e6e84c7 2404\r
a405b86d 2405 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2406\r
a405b86d 2407 @param[in] CheckPackage The package of parsed command line arguments.\r
2408 @param[in] KeyString The Key of the command line argument to check for.\r
94b17fa1 2409\r
a405b86d 2410 @retval NULL The flag is not on the command line.\r
2411 @retval !=NULL The pointer to unicode string of the value.\r
2412**/\r
94b17fa1 2413CONST CHAR16*\r
2414EFIAPI\r
2415ShellCommandLineGetValue (\r
2416 IN CONST LIST_ENTRY *CheckPackage,\r
2417 IN CHAR16 *KeyString\r
a405b86d 2418 )\r
2419{\r
94b17fa1 2420 LIST_ENTRY *Node;\r
252d9457 2421 CHAR16 *TempString;\r
94b17fa1 2422\r
2423 //\r
0c1950ba 2424 // return NULL for no package or KeyString is NULL\r
94b17fa1 2425 //\r
0c1950ba 2426 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2427 return (NULL);\r
2428 }\r
2429\r
2430 //\r
2431 // enumerate through the list of parametrs\r
2432 //\r
1e6e84c7 2433 for ( Node = GetFirstNode(CheckPackage)\r
2434 ; !IsNull (CheckPackage, Node)\r
2435 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2436 ){\r
94b17fa1 2437 //\r
252d9457 2438 // If the Name matches, return TRUE (and there may be NULL name)\r
94b17fa1 2439 //\r
2440 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2441 //\r
2442 // If Type is TypeStart then only compare the begining of the strings\r
2443 //\r
252d9457 2444 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2445 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2446 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2447 }\r
2448 TempString = NULL;\r
2449 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2450 if (TempString != NULL) {\r
2451 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2452 FreePool(TempString);\r
2453 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2454 }\r
2455 FreePool(TempString);\r
2456 }\r
2457 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2458 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2459 }\r
2460 }\r
2461 }\r
2462 return (NULL);\r
2463}\r
a405b86d 2464\r
94b17fa1 2465/**\r
a405b86d 2466 Returns raw value from command line argument.\r
94b17fa1 2467\r
a405b86d 2468 Raw value parameters are in the form of "value" in a specific position in the list.\r
1e6e84c7 2469\r
a405b86d 2470 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2471\r
a405b86d 2472 @param[in] CheckPackage The package of parsed command line arguments.\r
2473 @param[in] Position The position of the value.\r
94b17fa1 2474\r
a405b86d 2475 @retval NULL The flag is not on the command line.\r
2476 @retval !=NULL The pointer to unicode string of the value.\r
94b17fa1 2477 **/\r
2478CONST CHAR16*\r
2479EFIAPI\r
2480ShellCommandLineGetRawValue (\r
a405b86d 2481 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2482 IN UINTN Position\r
2483 )\r
2484{\r
94b17fa1 2485 LIST_ENTRY *Node;\r
2486\r
2487 //\r
2488 // check for CheckPackage == NULL\r
2489 //\r
2490 if (CheckPackage == NULL) {\r
2491 return (NULL);\r
2492 }\r
2493\r
2494 //\r
2495 // enumerate through the list of parametrs\r
2496 //\r
1e6e84c7 2497 for ( Node = GetFirstNode(CheckPackage)\r
2498 ; !IsNull (CheckPackage, Node)\r
2499 ; Node = GetNextNode(CheckPackage, Node)\r
a405b86d 2500 ){\r
94b17fa1 2501 //\r
2502 // If the position matches, return the value\r
2503 //\r
2504 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2505 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2506 }\r
2507 }\r
2508 return (NULL);\r
b1f95a06 2509}\r
2247dde4 2510\r
2511/**\r
1e6e84c7 2512 returns the number of command line value parameters that were parsed.\r
2513\r
2247dde4 2514 this will not include flags.\r
2515\r
a405b86d 2516 @param[in] CheckPackage The package of parsed command line arguments.\r
2517\r
2247dde4 2518 @retval (UINTN)-1 No parsing has ocurred\r
2519 @return other The number of value parameters found\r
2520**/\r
2521UINTN\r
2522EFIAPI\r
2523ShellCommandLineGetCount(\r
a405b86d 2524 IN CONST LIST_ENTRY *CheckPackage\r
125c2cf4 2525 )\r
2526{\r
a405b86d 2527 LIST_ENTRY *Node1;\r
2528 UINTN Count;\r
2529\r
2530 if (CheckPackage == NULL) {\r
2531 return (0);\r
2532 }\r
2533 for ( Node1 = GetFirstNode(CheckPackage), Count = 0\r
2534 ; !IsNull (CheckPackage, Node1)\r
2535 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2536 ){\r
2537 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {\r
2538 Count++;\r
2539 }\r
2540 }\r
2541 return (Count);\r
2247dde4 2542}\r
2543\r
36a9d672 2544/**\r
cceb4ebd 2545 Determines if a parameter is duplicated.\r
36a9d672 2546\r
1e6e84c7 2547 If Param is not NULL then it will point to a callee allocated string buffer\r
36a9d672 2548 with the parameter value if a duplicate is found.\r
2549\r
2550 If CheckPackage is NULL, then ASSERT.\r
2551\r
2552 @param[in] CheckPackage The package of parsed command line arguments.\r
2553 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2554\r
2555 @retval EFI_SUCCESS No parameters were duplicated.\r
2556 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2557 **/\r
2558EFI_STATUS\r
2559EFIAPI\r
2560ShellCommandLineCheckDuplicate (\r
2561 IN CONST LIST_ENTRY *CheckPackage,\r
2562 OUT CHAR16 **Param\r
2563 )\r
2564{\r
2565 LIST_ENTRY *Node1;\r
2566 LIST_ENTRY *Node2;\r
1e6e84c7 2567\r
36a9d672 2568 ASSERT(CheckPackage != NULL);\r
2569\r
1e6e84c7 2570 for ( Node1 = GetFirstNode(CheckPackage)\r
2571 ; !IsNull (CheckPackage, Node1)\r
2572 ; Node1 = GetNextNode(CheckPackage, Node1)\r
a405b86d 2573 ){\r
1e6e84c7 2574 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2575 ; !IsNull (CheckPackage, Node2)\r
2576 ; Node2 = GetNextNode(CheckPackage, Node2)\r
a405b86d 2577 ){\r
2578 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 2579 if (Param != NULL) {\r
2580 *Param = NULL;\r
2581 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2582 }\r
2583 return (EFI_DEVICE_ERROR);\r
2584 }\r
2585 }\r
2586 }\r
2587 return (EFI_SUCCESS);\r
2588}\r
2589\r
975136ab 2590/**\r
1e6e84c7 2591 This is a find and replace function. Upon successful return the NewString is a copy of\r
975136ab 2592 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2593\r
b3011f40 2594 If SourceString and NewString overlap the behavior is undefined.\r
2595\r
975136ab 2596 If the string would grow bigger than NewSize it will halt and return error.\r
2597\r
4ff7e37b
ED
2598 @param[in] SourceString The string with source buffer.\r
2599 @param[in, out] NewString The string with resultant buffer.\r
2600 @param[in] NewSize The size in bytes of NewString.\r
2601 @param[in] FindTarget The string to look for.\r
2602 @param[in] ReplaceWith The string to replace FindTarget with.\r
2603 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2604 immediately before it.\r
2605 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
969c783b 2606\r
2607 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2608 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2609 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2610 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2611 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2612 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
1e6e84c7 2613 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
969c783b 2614 the new string (truncation occurred).\r
a405b86d 2615 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
975136ab 2616**/\r
975136ab 2617EFI_STATUS\r
2618EFIAPI\r
a405b86d 2619ShellCopySearchAndReplace(\r
975136ab 2620 IN CHAR16 CONST *SourceString,\r
a405b86d 2621 IN OUT CHAR16 *NewString,\r
975136ab 2622 IN UINTN NewSize,\r
2623 IN CONST CHAR16 *FindTarget,\r
969c783b 2624 IN CONST CHAR16 *ReplaceWith,\r
a405b86d 2625 IN CONST BOOLEAN SkipPreCarrot,\r
2626 IN CONST BOOLEAN ParameterReplacing\r
1e6e84c7 2627 )\r
2247dde4 2628{\r
0158294b 2629 UINTN Size;\r
a405b86d 2630 CHAR16 *Replace;\r
2631\r
975136ab 2632 if ( (SourceString == NULL)\r
2633 || (NewString == NULL)\r
2634 || (FindTarget == NULL)\r
2635 || (ReplaceWith == NULL)\r
2636 || (StrLen(FindTarget) < 1)\r
2637 || (StrLen(SourceString) < 1)\r
a405b86d 2638 ){\r
975136ab 2639 return (EFI_INVALID_PARAMETER);\r
2640 }\r
a405b86d 2641 Replace = NULL;\r
2642 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {\r
2643 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
2644 } else {\r
2645 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
beab0fc5 2646 if (Replace != NULL) {\r
2647 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
2648 }\r
a405b86d 2649 }\r
3e082d58 2650 if (Replace == NULL) {\r
2651 return (EFI_OUT_OF_RESOURCES);\r
2652 }\r
98c16be5 2653 NewString = ZeroMem(NewString, NewSize);\r
2247dde4 2654 while (*SourceString != CHAR_NULL) {\r
969c783b 2655 //\r
a405b86d 2656 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
969c783b 2657 // dont have a carrot do a replace...\r
2658 //\r
1e6e84c7 2659 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
a405b86d 2660 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)\r
2661 ){\r
975136ab 2662 SourceString += StrLen(FindTarget);\r
0158294b 2663 Size = StrSize(NewString);\r
a405b86d 2664 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {\r
2665 FreePool(Replace);\r
975136ab 2666 return (EFI_BUFFER_TOO_SMALL);\r
2667 }\r
e75390f0 2668 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);\r
975136ab 2669 } else {\r
0158294b 2670 Size = StrSize(NewString);\r
2671 if (Size + sizeof(CHAR16) > NewSize) {\r
a405b86d 2672 FreePool(Replace);\r
975136ab 2673 return (EFI_BUFFER_TOO_SMALL);\r
2674 }\r
e75390f0 2675 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);\r
975136ab 2676 SourceString++;\r
2677 }\r
2678 }\r
a405b86d 2679 FreePool(Replace);\r
975136ab 2680 return (EFI_SUCCESS);\r
2681}\r
b1f95a06 2682\r
e2f8297f 2683/**\r
2684 Internal worker function to output a string.\r
2685\r
2686 This function will output a string to the correct StdOut.\r
2687\r
2688 @param[in] String The string to print out.\r
2689\r
2690 @retval EFI_SUCCESS The operation was sucessful.\r
2691 @retval !EFI_SUCCESS The operation failed.\r
2692**/\r
2693EFI_STATUS\r
2694EFIAPI\r
2695InternalPrintTo (\r
2696 IN CONST CHAR16 *String\r
2697 )\r
2698{\r
2699 UINTN Size;\r
2700 Size = StrSize(String) - sizeof(CHAR16);\r
a405b86d 2701 if (Size == 0) {\r
2702 return (EFI_SUCCESS);\r
2703 }\r
366f81a0 2704 if (gEfiShellParametersProtocol != NULL) {\r
2705 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
e2f8297f 2706 }\r
2707 if (mEfiShellInterface != NULL) {\r
06c355b4 2708 if (mEfiShellInterface->RedirArgc == 0) { \r
49bd498d 2709 //\r
2710 // Divide in half for old shell. Must be string length not size.\r
06c355b4 2711 // \r
2712 Size /=2; // Divide in half only when no redirection.\r
2713 }\r
a405b86d 2714 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
e2f8297f 2715 }\r
2716 ASSERT(FALSE);\r
2717 return (EFI_UNSUPPORTED);\r
2718}\r
2719\r
b1f95a06 2720/**\r
2721 Print at a specific location on the screen.\r
2722\r
f1b87e7a 2723 This function will move the cursor to a given screen location and print the specified string\r
1e6e84c7 2724\r
2725 If -1 is specified for either the Row or Col the current screen location for BOTH\r
f1b87e7a 2726 will be used.\r
b1f95a06 2727\r
2728 if either Row or Col is out of range for the current console, then ASSERT\r
2729 if Format is NULL, then ASSERT\r
2730\r
1e6e84c7 2731 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
b1f95a06 2732 the following additional flags:\r
2733 %N - Set output attribute to normal\r
2734 %H - Set output attribute to highlight\r
2735 %E - Set output attribute to error\r
2736 %B - Set output attribute to blue color\r
2737 %V - Set output attribute to green color\r
2738\r
2739 Note: The background color is controlled by the shell command cls.\r
2740\r
b1f95a06 2741 @param[in] Col the column to print at\r
252d9457 2742 @param[in] Row the row to print at\r
b1f95a06 2743 @param[in] Format the format string\r
2247dde4 2744 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2745\r
a405b86d 2746 @return EFI_SUCCESS The operation was successful.\r
2747 @return EFI_DEVICE_ERROR The console device reported an error.\r
b1f95a06 2748**/\r
a405b86d 2749EFI_STATUS\r
b1f95a06 2750EFIAPI\r
2247dde4 2751InternalShellPrintWorker(\r
b1f95a06 2752 IN INT32 Col OPTIONAL,\r
2753 IN INT32 Row OPTIONAL,\r
2754 IN CONST CHAR16 *Format,\r
252d9457 2755 IN VA_LIST Marker\r
1e6e84c7 2756 )\r
2247dde4 2757{\r
b1f95a06 2758 EFI_STATUS Status;\r
975136ab 2759 CHAR16 *ResumeLocation;\r
2760 CHAR16 *FormatWalker;\r
a405b86d 2761 UINTN OriginalAttribute;\r
89e8537a 2762 CHAR16 *mPostReplaceFormat;\r
2763 CHAR16 *mPostReplaceFormat2;\r
2764\r
2765 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2766 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
a405b86d 2767\r
f8d3e689 2768 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
2769 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2770 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2771 return (EFI_OUT_OF_RESOURCES);\r
2772 }\r
2773\r
a405b86d 2774 Status = EFI_SUCCESS;\r
2775 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
1e6e84c7 2776\r
975136ab 2777 //\r
2778 // Back and forth each time fixing up 1 of our flags...\r
2779 //\r
a405b86d 2780 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
975136ab 2781 ASSERT_EFI_ERROR(Status);\r
a405b86d 2782 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
975136ab 2783 ASSERT_EFI_ERROR(Status);\r
a405b86d 2784 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
975136ab 2785 ASSERT_EFI_ERROR(Status);\r
a405b86d 2786 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
975136ab 2787 ASSERT_EFI_ERROR(Status);\r
a405b86d 2788 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
975136ab 2789 ASSERT_EFI_ERROR(Status);\r
2790\r
2791 //\r
2792 // Use the last buffer from replacing to print from...\r
2793 //\r
a405b86d 2794 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
b1f95a06 2795\r
2796 if (Col != -1 && Row != -1) {\r
b1f95a06 2797 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
975136ab 2798 }\r
2799\r
ecd3d59f 2800 FormatWalker = mPostReplaceFormat2;\r
2247dde4 2801 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2802 //\r
2803 // Find the next attribute change request\r
2804 //\r
2805 ResumeLocation = StrStr(FormatWalker, L"%");\r
2806 if (ResumeLocation != NULL) {\r
2247dde4 2807 *ResumeLocation = CHAR_NULL;\r
975136ab 2808 }\r
2809 //\r
2810 // print the current FormatWalker string\r
2811 //\r
a405b86d 2812 if (StrLen(FormatWalker)>0) {\r
2813 Status = InternalPrintTo(FormatWalker);\r
2814 if (EFI_ERROR(Status)) {\r
2815 break;\r
2816 }\r
2817 }\r
2818\r
975136ab 2819 //\r
2820 // update the attribute\r
2821 //\r
2822 if (ResumeLocation != NULL) {\r
5d46f17b 2823 if (*(ResumeLocation-1) == L'^') {\r
8bb7441e 2824 //\r
2825 // Move cursor back 1 position to overwrite the ^\r
2826 //\r
2827 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);\r
2828\r
5d46f17b 2829 //\r
2830 // Print a simple '%' symbol\r
2831 //\r
2832 Status = InternalPrintTo(L"%");\r
2833 ResumeLocation = ResumeLocation - 1;\r
2834 } else {\r
2835 switch (*(ResumeLocation+1)) {\r
2836 case (L'N'):\r
2837 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
a405b86d 2838 break;\r
5d46f17b 2839 case (L'E'):\r
2840 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2841 break;\r
2842 case (L'H'):\r
2843 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2844 break;\r
2845 case (L'B'):\r
2846 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2847 break;\r
2848 case (L'V'):\r
2849 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2850 break;\r
2851 default:\r
2852 //\r
2853 // Print a simple '%' symbol\r
2854 //\r
2855 Status = InternalPrintTo(L"%");\r
2856 if (EFI_ERROR(Status)) {\r
2857 break;\r
2858 }\r
2859 ResumeLocation = ResumeLocation - 1;\r
2860 break;\r
2861 }\r
975136ab 2862 }\r
2863 } else {\r
2864 //\r
2865 // reset to normal now...\r
2866 //\r
975136ab 2867 break;\r
2868 }\r
2869\r
2870 //\r
2871 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2872 //\r
2873 FormatWalker = ResumeLocation + 2;\r
2874 }\r
b1f95a06 2875\r
a405b86d 2876 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
89e8537a 2877\r
2878 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2879 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
a405b86d 2880 return (Status);\r
5f7431d0 2881}\r
2247dde4 2882\r
2883/**\r
2884 Print at a specific location on the screen.\r
2885\r
e2f8297f 2886 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2887\r
2888 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2247dde4 2889 will be used.\r
2890\r
e2f8297f 2891 If either Row or Col is out of range for the current console, then ASSERT.\r
2892 If Format is NULL, then ASSERT.\r
2247dde4 2893\r
1e6e84c7 2894 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2895 the following additional flags:\r
2896 %N - Set output attribute to normal\r
2897 %H - Set output attribute to highlight\r
2898 %E - Set output attribute to error\r
2899 %B - Set output attribute to blue color\r
2900 %V - Set output attribute to green color\r
2901\r
2902 Note: The background color is controlled by the shell command cls.\r
2903\r
2247dde4 2904 @param[in] Col the column to print at\r
a405b86d 2905 @param[in] Row the row to print at\r
2247dde4 2906 @param[in] Format the format string\r
a405b86d 2907 @param[in] ... The variable argument list.\r
2247dde4 2908\r
a405b86d 2909 @return EFI_SUCCESS The printing was successful.\r
2910 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2911**/\r
a405b86d 2912EFI_STATUS\r
2247dde4 2913EFIAPI\r
2914ShellPrintEx(\r
2915 IN INT32 Col OPTIONAL,\r
2916 IN INT32 Row OPTIONAL,\r
2917 IN CONST CHAR16 *Format,\r
2918 ...\r
1e6e84c7 2919 )\r
2247dde4 2920{\r
2921 VA_LIST Marker;\r
a405b86d 2922 EFI_STATUS RetVal;\r
3e082d58 2923 if (Format == NULL) {\r
2924 return (EFI_INVALID_PARAMETER);\r
2925 }\r
2247dde4 2926 VA_START (Marker, Format);\r
a405b86d 2927 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
e2f8297f 2928 VA_END(Marker);\r
a405b86d 2929 return(RetVal);\r
2247dde4 2930}\r
2931\r
2932/**\r
2933 Print at a specific location on the screen.\r
2934\r
e2f8297f 2935 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2936\r
2937 If -1 is specified for either the Row or Col the current screen location for BOTH\r
e2f8297f 2938 will be used.\r
2247dde4 2939\r
e2f8297f 2940 If either Row or Col is out of range for the current console, then ASSERT.\r
2941 If Format is NULL, then ASSERT.\r
2247dde4 2942\r
1e6e84c7 2943 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2944 the following additional flags:\r
1e6e84c7 2945 %N - Set output attribute to normal.\r
2946 %H - Set output attribute to highlight.\r
2947 %E - Set output attribute to error.\r
2948 %B - Set output attribute to blue color.\r
2949 %V - Set output attribute to green color.\r
2247dde4 2950\r
2951 Note: The background color is controlled by the shell command cls.\r
2952\r
1e6e84c7 2953 @param[in] Col The column to print at.\r
a405b86d 2954 @param[in] Row The row to print at.\r
1e6e84c7 2955 @param[in] Language The language of the string to retrieve. If this parameter\r
2956 is NULL, then the current platform language is used.\r
2957 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2958 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
a405b86d 2959 @param[in] ... The variable argument list.\r
2247dde4 2960\r
a405b86d 2961 @return EFI_SUCCESS The printing was successful.\r
2962 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2963**/\r
a405b86d 2964EFI_STATUS\r
2247dde4 2965EFIAPI\r
2966ShellPrintHiiEx(\r
2967 IN INT32 Col OPTIONAL,\r
2968 IN INT32 Row OPTIONAL,\r
1e6e84c7 2969 IN CONST CHAR8 *Language OPTIONAL,\r
2247dde4 2970 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2971 IN CONST EFI_HANDLE HiiFormatHandle,\r
2972 ...\r
2973 )\r
2974{\r
2975 VA_LIST Marker;\r
2976 CHAR16 *HiiFormatString;\r
a405b86d 2977 EFI_STATUS RetVal;\r
2247dde4 2978\r
2979 VA_START (Marker, HiiFormatHandle);\r
1e6e84c7 2980 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2247dde4 2981 ASSERT(HiiFormatString != NULL);\r
2982\r
2983 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2984\r
a405b86d 2985 SHELL_FREE_NON_NULL(HiiFormatString);\r
e2f8297f 2986 VA_END(Marker);\r
2247dde4 2987\r
2988 return (RetVal);\r
2989}\r
2990\r
2991/**\r
2992 Function to determine if a given filename represents a file or a directory.\r
2993\r
2994 @param[in] DirName Path to directory to test.\r
2995\r
c8c22591 2996 @retval EFI_SUCCESS The Path represents a directory\r
2997 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2998 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2999 @return The path failed to open\r
2247dde4 3000**/\r
3001EFI_STATUS\r
3002EFIAPI\r
3003ShellIsDirectory(\r
3004 IN CONST CHAR16 *DirName\r
3005 )\r
3006{\r
3007 EFI_STATUS Status;\r
a405b86d 3008 SHELL_FILE_HANDLE Handle;\r
3e082d58 3009 CHAR16 *TempLocation;\r
3010 CHAR16 *TempLocation2;\r
2247dde4 3011\r
ecd3d59f 3012 ASSERT(DirName != NULL);\r
3013\r
a405b86d 3014 Handle = NULL;\r
3015 TempLocation = NULL;\r
2247dde4 3016\r
3017 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
3018 if (EFI_ERROR(Status)) {\r
a405b86d 3019 //\r
3020 // try good logic first.\r
3021 //\r
366f81a0 3022 if (gEfiShellProtocol != NULL) {\r
3e082d58 3023 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
c8c22591 3024 if (TempLocation == NULL) {\r
3025 ShellCloseFile(&Handle);\r
3026 return (EFI_OUT_OF_RESOURCES);\r
3027 }\r
3e082d58 3028 TempLocation2 = StrStr(TempLocation, L":");\r
3029 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
3030 *(TempLocation2+1) = CHAR_NULL;\r
a405b86d 3031 }\r
366f81a0 3032 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
a405b86d 3033 FreePool(TempLocation);\r
3034 return (EFI_SUCCESS);\r
3035 }\r
3036 FreePool(TempLocation);\r
3037 } else {\r
3038 //\r
3039 // probably a map name?!?!!?\r
3040 //\r
3041 TempLocation = StrStr(DirName, L"\\");\r
3042 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {\r
3043 return (EFI_SUCCESS);\r
3044 }\r
3045 }\r
2247dde4 3046 return (Status);\r
3047 }\r
3048\r
3049 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
3050 ShellCloseFile(&Handle);\r
3051 return (EFI_SUCCESS);\r
3052 }\r
3053 ShellCloseFile(&Handle);\r
3054 return (EFI_NOT_FOUND);\r
3055}\r
3056\r
36a9d672 3057/**\r
3058 Function to determine if a given filename represents a file.\r
3059\r
3060 @param[in] Name Path to file to test.\r
3061\r
3062 @retval EFI_SUCCESS The Path represents a file.\r
3063 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3064 @retval other The path failed to open.\r
3065**/\r
3066EFI_STATUS\r
3067EFIAPI\r
3068ShellIsFile(\r
3069 IN CONST CHAR16 *Name\r
3070 )\r
3071{\r
3072 EFI_STATUS Status;\r
a405b86d 3073 SHELL_FILE_HANDLE Handle;\r
36a9d672 3074\r
ecd3d59f 3075 ASSERT(Name != NULL);\r
3076\r
36a9d672 3077 Handle = NULL;\r
3078\r
3079 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
3080 if (EFI_ERROR(Status)) {\r
3081 return (Status);\r
3082 }\r
3083\r
3084 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
3085 ShellCloseFile(&Handle);\r
3086 return (EFI_SUCCESS);\r
3087 }\r
3088 ShellCloseFile(&Handle);\r
3089 return (EFI_NOT_FOUND);\r
3090}\r
3091\r
b3011f40 3092/**\r
3093 Function to determine if a given filename represents a file.\r
3094\r
3095 This will search the CWD and then the Path.\r
3096\r
3097 If Name is NULL, then ASSERT.\r
3098\r
3099 @param[in] Name Path to file to test.\r
3100\r
3101 @retval EFI_SUCCESS The Path represents a file.\r
3102 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3103 @retval other The path failed to open.\r
3104**/\r
3105EFI_STATUS\r
3106EFIAPI\r
3107ShellIsFileInPath(\r
3108 IN CONST CHAR16 *Name\r
a405b86d 3109 )\r
3110{\r
b3011f40 3111 CHAR16 *NewName;\r
3112 EFI_STATUS Status;\r
3113\r
3114 if (!EFI_ERROR(ShellIsFile(Name))) {\r
a405b86d 3115 return (EFI_SUCCESS);\r
b3011f40 3116 }\r
3117\r
3118 NewName = ShellFindFilePath(Name);\r
3119 if (NewName == NULL) {\r
3120 return (EFI_NOT_FOUND);\r
3121 }\r
3122 Status = ShellIsFile(NewName);\r
3123 FreePool(NewName);\r
3124 return (Status);\r
3125}\r
252d9457 3126\r
74b0fb8c
JC
3127/**\r
3128 Function return the number converted from a hex representation of a number.\r
3129\r
3130 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid\r
3131 result. Use ShellConvertStringToUint64 instead.\r
3132\r
3133 @param[in] String String representation of a number.\r
3134\r
3135 @return The unsigned integer result of the conversion.\r
3136 @retval (UINTN)(-1) An error occured.\r
3137**/\r
3138UINTN\r
3139EFIAPI\r
3140ShellHexStrToUintn(\r
3141 IN CONST CHAR16 *String\r
3142 )\r
3143{\r
3144 UINT64 RetVal;\r
3145\r
3146 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {\r
3147 return ((UINTN)RetVal);\r
3148 }\r
3149 \r
3150 return ((UINTN)(-1));\r
3151}\r
3152\r
125c2cf4 3153/**\r
1e6e84c7 3154 Function to determine whether a string is decimal or hex representation of a number\r
d59fc244 3155 and return the number converted from the string. Spaces are always skipped.\r
125c2cf4 3156\r
3157 @param[in] String String representation of a number\r
3158\r
252d9457 3159 @return the number\r
3160 @retval (UINTN)(-1) An error ocurred.\r
125c2cf4 3161**/\r
3162UINTN\r
3163EFIAPI\r
3164ShellStrToUintn(\r
3165 IN CONST CHAR16 *String\r
3166 )\r
3167{\r
252d9457 3168 UINT64 RetVal;\r
3169 BOOLEAN Hex;\r
3170\r
3171 Hex = FALSE;\r
3172\r
658bf43e 3173 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {\r
252d9457 3174 Hex = TRUE;\r
125c2cf4 3175 }\r
252d9457 3176\r
3177 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
3178 return ((UINTN)RetVal);\r
3179 }\r
3180 return ((UINTN)(-1));\r
125c2cf4 3181}\r
3182\r
3183/**\r
3184 Safely append with automatic string resizing given length of Destination and\r
3185 desired length of copy from Source.\r
3186\r
3187 append the first D characters of Source to the end of Destination, where D is\r
3188 the lesser of Count and the StrLen() of Source. If appending those D characters\r
3189 will fit within Destination (whose Size is given as CurrentSize) and\r
1e6e84c7 3190 still leave room for a NULL terminator, then those characters are appended,\r
3191 starting at the original terminating NULL of Destination, and a new terminating\r
3192 NULL is appended.\r
125c2cf4 3193\r
3194 If appending D characters onto Destination will result in a overflow of the size\r
3195 given in CurrentSize the string will be grown such that the copy can be performed\r
3196 and CurrentSize will be updated to the new size.\r
3197\r
3198 If Source is NULL, there is nothing to append, just return the current buffer in\r
3199 Destination.\r
3200\r
3201 if Destination is NULL, then ASSERT()\r
3202 if Destination's current length (including NULL terminator) is already more then\r
3203 CurrentSize, then ASSERT()\r
3204\r
4ff7e37b
ED
3205 @param[in, out] Destination The String to append onto\r
3206 @param[in, out] CurrentSize on call the number of bytes in Destination. On\r
125c2cf4 3207 return possibly the new size (still in bytes). if NULL\r
3208 then allocate whatever is needed.\r
3209 @param[in] Source The String to append from\r
3210 @param[in] Count Maximum number of characters to append. if 0 then\r
3211 all are appended.\r
3212\r
3213 @return Destination return the resultant string.\r
3214**/\r
3215CHAR16*\r
3216EFIAPI\r
3217StrnCatGrow (\r
3218 IN OUT CHAR16 **Destination,\r
3219 IN OUT UINTN *CurrentSize,\r
3220 IN CONST CHAR16 *Source,\r
3221 IN UINTN Count\r
3222 )\r
3223{\r
3224 UINTN DestinationStartSize;\r
3225 UINTN NewSize;\r
3226\r
3227 //\r
3228 // ASSERTs\r
3229 //\r
3230 ASSERT(Destination != NULL);\r
3231\r
3232 //\r
3233 // If there's nothing to do then just return Destination\r
3234 //\r
3235 if (Source == NULL) {\r
3236 return (*Destination);\r
3237 }\r
3238\r
3239 //\r
3240 // allow for un-initialized pointers, based on size being 0\r
3241 //\r
3242 if (CurrentSize != NULL && *CurrentSize == 0) {\r
3243 *Destination = NULL;\r
3244 }\r
3245\r
3246 //\r
3247 // allow for NULL pointers address as Destination\r
3248 //\r
3249 if (*Destination != NULL) {\r
3250 ASSERT(CurrentSize != 0);\r
3251 DestinationStartSize = StrSize(*Destination);\r
3252 ASSERT(DestinationStartSize <= *CurrentSize);\r
3253 } else {\r
3254 DestinationStartSize = 0;\r
3255// ASSERT(*CurrentSize == 0);\r
3256 }\r
3257\r
3258 //\r
3259 // Append all of Source?\r
3260 //\r
3261 if (Count == 0) {\r
3262 Count = StrLen(Source);\r
3263 }\r
3264\r
3265 //\r
3266 // Test and grow if required\r
3267 //\r
3268 if (CurrentSize != NULL) {\r
3269 NewSize = *CurrentSize;\r
f480fdc0
ED
3270 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {\r
3271 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
3272 NewSize += 2 * Count * sizeof(CHAR16);\r
3273 }\r
3274 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
3275 *CurrentSize = NewSize;\r
125c2cf4 3276 }\r
125c2cf4 3277 } else {\r
c587fd3e
QS
3278 NewSize = (Count+1)*sizeof(CHAR16);\r
3279 *Destination = AllocateZeroPool(NewSize);\r
125c2cf4 3280 }\r
3281\r
3282 //\r
3283 // Now use standard StrnCat on a big enough buffer\r
3284 //\r
c9d92df0 3285 if (*Destination == NULL) {\r
3286 return (NULL);\r
3287 }\r
e75390f0 3288 \r
c587fd3e 3289 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);\r
e75390f0 3290 return *Destination;\r
125c2cf4 3291}\r
c9d92df0 3292\r
3293/**\r
3294 Prompt the user and return the resultant answer to the requestor.\r
3295\r
3296 This function will display the requested question on the shell prompt and then\r
1d322461 3297 wait for an appropriate answer to be input from the console.\r
c9d92df0 3298\r
a405b86d 3299 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
c9d92df0 3300 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3301\r
a405b86d 3302 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
c9d92df0 3303 CHAR16*.\r
3304\r
3305 In either case *Response must be callee freed if Response was not NULL;\r
3306\r
3307 @param Type What type of question is asked. This is used to filter the input\r
3308 to prevent invalid answers to question.\r
3309 @param Prompt Pointer to string prompt to use to request input.\r
3310 @param Response Pointer to Response which will be populated upon return.\r
3311\r
3312 @retval EFI_SUCCESS The operation was sucessful.\r
3313 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3314 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3315 @return other The operation failed.\r
3316**/\r
3317EFI_STATUS\r
3318EFIAPI\r
3319ShellPromptForResponse (\r
3320 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3321 IN CHAR16 *Prompt OPTIONAL,\r
3322 IN OUT VOID **Response OPTIONAL\r
3323 )\r
3324{\r
3325 EFI_STATUS Status;\r
3326 EFI_INPUT_KEY Key;\r
3327 UINTN EventIndex;\r
3328 SHELL_PROMPT_RESPONSE *Resp;\r
a405b86d 3329 UINTN Size;\r
3330 CHAR16 *Buffer;\r
c9d92df0 3331\r
a405b86d 3332 Status = EFI_UNSUPPORTED;\r
3333 Resp = NULL;\r
3334 Buffer = NULL;\r
3335 Size = 0;\r
3336 if (Type != ShellPromptResponseTypeFreeform) {\r
252d9457 3337 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
3e082d58 3338 if (Resp == NULL) {\r
3339 return (EFI_OUT_OF_RESOURCES);\r
3340 }\r
90bfa227 3341 }\r
c9d92df0 3342\r
3343 switch(Type) {\r
a405b86d 3344 case ShellPromptResponseTypeQuitContinue:\r
c9d92df0 3345 if (Prompt != NULL) {\r
3346 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3347 }\r
3348 //\r
3349 // wait for valid response\r
3350 //\r
3351 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3352 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3353 if (EFI_ERROR(Status)) {\r
3354 break;\r
3355 }\r
c9d92df0 3356 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3357 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r
a405b86d 3358 *Resp = ShellPromptResponseQuit;\r
c9d92df0 3359 } else {\r
a405b86d 3360 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3361 }\r
3362 break;\r
a405b86d 3363 case ShellPromptResponseTypeYesNoCancel:\r
c9d92df0 3364 if (Prompt != NULL) {\r
3365 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3366 }\r
3367 //\r
3368 // wait for valid response\r
3369 //\r
a405b86d 3370 *Resp = ShellPromptResponseMax;\r
3371 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3372 if (ShellGetExecutionBreakFlag()) {\r
3373 Status = EFI_ABORTED;\r
3374 break;\r
3375 }\r
c9d92df0 3376 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3377 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3378 if (EFI_ERROR(Status)) {\r
3379 break;\r
3380 }\r
c9d92df0 3381 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3382 switch (Key.UnicodeChar) {\r
3383 case L'Y':\r
3384 case L'y':\r
a405b86d 3385 *Resp = ShellPromptResponseYes;\r
c9d92df0 3386 break;\r
3387 case L'N':\r
3388 case L'n':\r
a405b86d 3389 *Resp = ShellPromptResponseNo;\r
3390 break;\r
3391 case L'C':\r
3392 case L'c':\r
3393 *Resp = ShellPromptResponseCancel;\r
3394 break;\r
3395 }\r
3396 }\r
3397 break; case ShellPromptResponseTypeYesNoAllCancel:\r
3398 if (Prompt != NULL) {\r
3399 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3400 }\r
3401 //\r
3402 // wait for valid response\r
3403 //\r
3404 *Resp = ShellPromptResponseMax;\r
3405 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3406 if (ShellGetExecutionBreakFlag()) {\r
3407 Status = EFI_ABORTED;\r
3408 break;\r
3409 }\r
a405b86d 3410 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3411 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3412 if (EFI_ERROR(Status)) {\r
3413 break;\r
3414 }\r
a405b86d 3415 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3416 switch (Key.UnicodeChar) {\r
3417 case L'Y':\r
3418 case L'y':\r
3419 *Resp = ShellPromptResponseYes;\r
3420 break;\r
3421 case L'N':\r
3422 case L'n':\r
3423 *Resp = ShellPromptResponseNo;\r
c9d92df0 3424 break;\r
3425 case L'A':\r
3426 case L'a':\r
a405b86d 3427 *Resp = ShellPromptResponseAll;\r
c9d92df0 3428 break;\r
3429 case L'C':\r
3430 case L'c':\r
a405b86d 3431 *Resp = ShellPromptResponseCancel;\r
c9d92df0 3432 break;\r
3433 }\r
3434 }\r
3435 break;\r
a405b86d 3436 case ShellPromptResponseTypeEnterContinue:\r
3437 case ShellPromptResponseTypeAnyKeyContinue:\r
c9d92df0 3438 if (Prompt != NULL) {\r
3439 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3440 }\r
3441 //\r
3442 // wait for valid response\r
3443 //\r
a405b86d 3444 *Resp = ShellPromptResponseMax;\r
3445 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3446 if (ShellGetExecutionBreakFlag()) {\r
3447 Status = EFI_ABORTED;\r
3448 break;\r
3449 }\r
c9d92df0 3450 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
a405b86d 3451 if (Type == ShellPromptResponseTypeEnterContinue) {\r
c9d92df0 3452 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3453 if (EFI_ERROR(Status)) {\r
3454 break;\r
3455 }\r
c9d92df0 3456 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3457 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
a405b86d 3458 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3459 break;\r
3460 }\r
3461 }\r
a405b86d 3462 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3463 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3464 ASSERT_EFI_ERROR(Status);\r
3465 *Resp = ShellPromptResponseContinue;\r
3466 break;\r
3467 }\r
3468 }\r
3469 break;\r
3470 case ShellPromptResponseTypeYesNo:\r
3471 if (Prompt != NULL) {\r
3472 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3473 }\r
3474 //\r
3475 // wait for valid response\r
3476 //\r
3477 *Resp = ShellPromptResponseMax;\r
3478 while (*Resp == ShellPromptResponseMax) {\r
194ae48d
JC
3479 if (ShellGetExecutionBreakFlag()) {\r
3480 Status = EFI_ABORTED;\r
3481 break;\r
3482 }\r
a405b86d 3483 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3484 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3485 if (EFI_ERROR(Status)) {\r
3486 break;\r
3487 }\r
a405b86d 3488 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3489 switch (Key.UnicodeChar) {\r
3490 case L'Y':\r
3491 case L'y':\r
3492 *Resp = ShellPromptResponseYes;\r
3493 break;\r
3494 case L'N':\r
3495 case L'n':\r
3496 *Resp = ShellPromptResponseNo;\r
3497 break;\r
3498 }\r
3499 }\r
3500 break;\r
3501 case ShellPromptResponseTypeFreeform:\r
3502 if (Prompt != NULL) {\r
3503 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3504 }\r
3505 while(1) {\r
194ae48d
JC
3506 if (ShellGetExecutionBreakFlag()) {\r
3507 Status = EFI_ABORTED;\r
3508 break;\r
3509 }\r
a405b86d 3510 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3511 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
31b018a6
JC
3512 if (EFI_ERROR(Status)) {\r
3513 break;\r
3514 }\r
a405b86d 3515 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3516 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
c9d92df0 3517 break;\r
3518 }\r
a405b86d 3519 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3520 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);\r
c9d92df0 3521 }\r
3522 break;\r
a405b86d 3523 //\r
3524 // This is the location to add new prompt types.\r
194ae48d 3525 // If your new type loops remember to add ExecutionBreak support.\r
a405b86d 3526 //\r
c9d92df0 3527 default:\r
a405b86d 3528 ASSERT(FALSE);\r
c9d92df0 3529 }\r
3530\r
3531 if (Response != NULL) {\r
a405b86d 3532 if (Resp != NULL) {\r
3533 *Response = Resp;\r
3534 } else if (Buffer != NULL) {\r
3535 *Response = Buffer;\r
3536 }\r
c9d92df0 3537 } else {\r
a405b86d 3538 if (Resp != NULL) {\r
3539 FreePool(Resp);\r
3540 }\r
3541 if (Buffer != NULL) {\r
3542 FreePool(Buffer);\r
3543 }\r
c9d92df0 3544 }\r
3545\r
a405b86d 3546 ShellPrintEx(-1, -1, L"\r\n");\r
c9d92df0 3547 return (Status);\r
3548}\r
3549\r
3550/**\r
3551 Prompt the user and return the resultant answer to the requestor.\r
3552\r
3553 This function is the same as ShellPromptForResponse, except that the prompt is\r
3554 automatically pulled from HII.\r
3555\r
3556 @param Type What type of question is asked. This is used to filter the input\r
3557 to prevent invalid answers to question.\r
a405b86d 3558 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3559 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3560 @param Response Pointer to Response which will be populated upon return.\r
c9d92df0 3561\r
3562 @retval EFI_SUCCESS the operation was sucessful.\r
3563 @return other the operation failed.\r
3564\r
3565 @sa ShellPromptForResponse\r
3566**/\r
3567EFI_STATUS\r
3568EFIAPI\r
3569ShellPromptForResponseHii (\r
3570 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3571 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3572 IN CONST EFI_HANDLE HiiFormatHandle,\r
3573 IN OUT VOID **Response\r
3574 )\r
3575{\r
3576 CHAR16 *Prompt;\r
3577 EFI_STATUS Status;\r
3578\r
3579 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
3580 Status = ShellPromptForResponse(Type, Prompt, Response);\r
3581 FreePool(Prompt);\r
3582 return (Status);\r
3583}\r
3584\r
a405b86d 3585/**\r
3586 Function to determin if an entire string is a valid number.\r
3587\r
3588 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
c9d92df0 3589\r
a405b86d 3590 @param[in] String The string to evaluate.\r
3591 @param[in] ForceHex TRUE - always assume hex.\r
3592 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
658bf43e 3593 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.\r
a405b86d 3594\r
3595 @retval TRUE It is all numeric (dec/hex) characters.\r
3596 @retval FALSE There is a non-numeric character.\r
3597**/\r
3598BOOLEAN\r
3599EFIAPI\r
252d9457 3600InternalShellIsHexOrDecimalNumber (\r
a405b86d 3601 IN CONST CHAR16 *String,\r
3602 IN CONST BOOLEAN ForceHex,\r
658bf43e 3603 IN CONST BOOLEAN StopAtSpace,\r
3604 IN CONST BOOLEAN TimeNumbers\r
a405b86d 3605 )\r
3606{\r
3607 BOOLEAN Hex;\r
3608\r
3609 //\r
3610 // chop off a single negative sign\r
3611 //\r
3612 if (String != NULL && *String == L'-') {\r
3613 String++;\r
3614 }\r
b0934ac4 3615\r
a405b86d 3616 if (String == NULL) {\r
3617 return (FALSE);\r
3618 }\r
3619\r
3620 //\r
3621 // chop leading zeroes\r
3622 //\r
3623 while(String != NULL && *String == L'0'){\r
3624 String++;\r
3625 }\r
3626 //\r
3627 // allow '0x' or '0X', but not 'x' or 'X'\r
3628 //\r
3629 if (String != NULL && (*String == L'x' || *String == L'X')) {\r
3630 if (*(String-1) != L'0') {\r
3631 //\r
3632 // we got an x without a preceeding 0\r
3633 //\r
3634 return (FALSE);\r
3635 }\r
3636 String++;\r
3637 Hex = TRUE;\r
3638 } else if (ForceHex) {\r
3639 Hex = TRUE;\r
3640 } else {\r
3641 Hex = FALSE;\r
3642 }\r
3643\r
3644 //\r
3645 // loop through the remaining characters and use the lib function\r
3646 //\r
3647 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
658bf43e 3648 if (TimeNumbers && (String[0] == L':')) {\r
3649 continue;\r
3650 }\r
a405b86d 3651 if (Hex) {\r
3652 if (!ShellIsHexaDecimalDigitCharacter(*String)) {\r
3653 return (FALSE);\r
3654 }\r
3655 } else {\r
3656 if (!ShellIsDecimalDigitCharacter(*String)) {\r
3657 return (FALSE);\r
3658 }\r
3659 }\r
3660 }\r
252d9457 3661\r
a405b86d 3662 return (TRUE);\r
3663}\r
3664\r
3665/**\r
3666 Function to determine if a given filename exists.\r
3667\r
3668 @param[in] Name Path to test.\r
3669\r
3670 @retval EFI_SUCCESS The Path represents a file.\r
3671 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3672 @retval other The path failed to open.\r
3673**/\r
3674EFI_STATUS\r
3675EFIAPI\r
3676ShellFileExists(\r
3677 IN CONST CHAR16 *Name\r
3678 )\r
3679{\r
3680 EFI_STATUS Status;\r
3681 EFI_SHELL_FILE_INFO *List;\r
3682\r
3683 ASSERT(Name != NULL);\r
3684\r
3685 List = NULL;\r
3686 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);\r
3687 if (EFI_ERROR(Status)) {\r
3688 return (Status);\r
3689 }\r
3690\r
3691 ShellCloseFileMetaArg(&List);\r
3692\r
3693 return (EFI_SUCCESS);\r
3694}\r
252d9457 3695\r
3696/**\r
b0934ac4 3697 Convert a Unicode character to upper case only if\r
252d9457 3698 it maps to a valid small-case ASCII character.\r
3699\r
3700 This internal function only deal with Unicode character\r
3701 which maps to a valid small-case ASCII character, i.e.\r
3702 L'a' to L'z'. For other Unicode character, the input character\r
3703 is returned directly.\r
3704\r
3705 @param Char The character to convert.\r
3706\r
3707 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
3708 @retval Unchanged Otherwise.\r
3709\r
3710**/\r
3711CHAR16\r
3712EFIAPI\r
3713InternalShellCharToUpper (\r
3714 IN CHAR16 Char\r
3715 )\r
3716{\r
3717 if (Char >= L'a' && Char <= L'z') {\r
3718 return (CHAR16) (Char - (L'a' - L'A'));\r
3719 }\r
3720\r
3721 return Char;\r
3722}\r
3723\r
3724/**\r
3725 Convert a Unicode character to numerical value.\r
3726\r
3727 This internal function only deal with Unicode character\r
3728 which maps to a valid hexadecimal ASII character, i.e.\r
b0934ac4 3729 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other\r
252d9457 3730 Unicode character, the value returned does not make sense.\r
3731\r
3732 @param Char The character to convert.\r
3733\r
3734 @return The numerical value converted.\r
3735\r
3736**/\r
3737UINTN\r
3738EFIAPI\r
3739InternalShellHexCharToUintn (\r
3740 IN CHAR16 Char\r
3741 )\r
3742{\r
3743 if (ShellIsDecimalDigitCharacter (Char)) {\r
3744 return Char - L'0';\r
3745 }\r
3746\r
3747 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
3748}\r
3749\r
3750/**\r
3751 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3752\r
80f3e34f 3753 This function returns a value of type UINT64 by interpreting the contents\r
252d9457 3754 of the Unicode string specified by String as a hexadecimal number.\r
3755 The format of the input Unicode string String is:\r
3756\r
3757 [spaces][zeros][x][hexadecimal digits].\r
3758\r
3759 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3760 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3761 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3762 The function will ignore the pad space, which includes spaces or tab characters,\r
3763 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3764 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3765 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3766 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3767\r
3768 If String has only pad spaces, then zero is returned.\r
3769 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3770 then zero is returned.\r
3771\r
3772 @param[in] String A pointer to a Null-terminated Unicode string.\r
3773 @param[out] Value Upon a successful return the value of the conversion.\r
3774 @param[in] StopAtSpace FALSE to skip spaces.\r
3775\r
3776 @retval EFI_SUCCESS The conversion was successful.\r
3777 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3778 @retval EFI_DEVICE_ERROR An overflow occured.\r
3779**/\r
3780EFI_STATUS\r
3781EFIAPI\r
3782InternalShellStrHexToUint64 (\r
3783 IN CONST CHAR16 *String,\r
3784 OUT UINT64 *Value,\r
3785 IN CONST BOOLEAN StopAtSpace\r
3786 )\r
3787{\r
3788 UINT64 Result;\r
3789\r
3790 if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
3791 return (EFI_INVALID_PARAMETER);\r
3792 }\r
b0934ac4 3793\r
252d9457 3794 //\r
b0934ac4 3795 // Ignore the pad spaces (space or tab)\r
252d9457 3796 //\r
3797 while ((*String == L' ') || (*String == L'\t')) {\r
3798 String++;\r
3799 }\r
3800\r
3801 //\r
3802 // Ignore leading Zeros after the spaces\r
3803 //\r
3804 while (*String == L'0') {\r
3805 String++;\r
3806 }\r
3807\r
3808 if (InternalShellCharToUpper (*String) == L'X') {\r
3809 if (*(String - 1) != L'0') {\r
3810 return 0;\r
3811 }\r
3812 //\r
3813 // Skip the 'X'\r
3814 //\r
3815 String++;\r
3816 }\r
3817\r
3818 Result = 0;\r
b0934ac4 3819\r
252d9457 3820 //\r
80f3e34f 3821 // there is a space where there should't be\r
252d9457 3822 //\r
80f3e34f
JC
3823 if (*String == L' ') {\r
3824 return (EFI_INVALID_PARAMETER);\r
252d9457 3825 }\r
b0934ac4 3826\r
252d9457 3827 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
3828 //\r
b0934ac4 3829 // If the Hex Number represented by String overflows according\r
80f3e34f 3830 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.\r
252d9457 3831 //\r
3832 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
3833// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
3834 return (EFI_DEVICE_ERROR);\r
3835 }\r
3836\r
3837 Result = (LShiftU64(Result, 4));\r
3838 Result += InternalShellHexCharToUintn (*String);\r
3839 String++;\r
3840\r
3841 //\r
49bd498d 3842 // stop at spaces if requested\r
252d9457 3843 //\r
49bd498d 3844 if (StopAtSpace && *String == L' ') {\r
3845 break;\r
252d9457 3846 }\r
3847 }\r
3848\r
3849 *Value = Result;\r
3850 return (EFI_SUCCESS);\r
3851}\r
3852\r
3853/**\r
3854 Convert a Null-terminated Unicode decimal string to a value of\r
3855 type UINT64.\r
3856\r
3857 This function returns a value of type UINT64 by interpreting the contents\r
3858 of the Unicode string specified by String as a decimal number. The format\r
3859 of the input Unicode string String is:\r
3860\r
3861 [spaces] [decimal digits].\r
3862\r
3863 The valid decimal digit character is in the range [0-9]. The\r
3864 function will ignore the pad space, which includes spaces or\r
3865 tab characters, before [decimal digits]. The running zero in the\r
3866 beginning of [decimal digits] will be ignored. Then, the function\r
3867 stops at the first character that is a not a valid decimal character\r
3868 or a Null-terminator, whichever one comes first.\r
3869\r
3870 If String has only pad spaces, then 0 is returned.\r
3871 If String has no pad spaces or valid decimal digits,\r
3872 then 0 is returned.\r
3873\r
3874 @param[in] String A pointer to a Null-terminated Unicode string.\r
3875 @param[out] Value Upon a successful return the value of the conversion.\r
3876 @param[in] StopAtSpace FALSE to skip spaces.\r
3877\r
3878 @retval EFI_SUCCESS The conversion was successful.\r
3879 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3880 @retval EFI_DEVICE_ERROR An overflow occured.\r
3881**/\r
3882EFI_STATUS\r
3883EFIAPI\r
3884InternalShellStrDecimalToUint64 (\r
3885 IN CONST CHAR16 *String,\r
3886 OUT UINT64 *Value,\r
3887 IN CONST BOOLEAN StopAtSpace\r
3888 )\r
3889{\r
3890 UINT64 Result;\r
3891\r
3892 if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
3893 return (EFI_INVALID_PARAMETER);\r
3894 }\r
3895\r
3896 //\r
3897 // Ignore the pad spaces (space or tab)\r
3898 //\r
3899 while ((*String == L' ') || (*String == L'\t')) {\r
3900 String++;\r
3901 }\r
3902\r
3903 //\r
3904 // Ignore leading Zeros after the spaces\r
3905 //\r
3906 while (*String == L'0') {\r
3907 String++;\r
3908 }\r
3909\r
3910 Result = 0;\r
3911\r
3912 //\r
80f3e34f
JC
3913 // Stop upon space if requested \r
3914 // (if the whole value was 0)\r
252d9457 3915 //\r
80f3e34f
JC
3916 if (StopAtSpace && *String == L' ') {\r
3917 *Value = Result;\r
3918 return (EFI_SUCCESS);\r
252d9457 3919 }\r
80f3e34f 3920\r
252d9457 3921 while (ShellIsDecimalDigitCharacter (*String)) {\r
3922 //\r
b0934ac4 3923 // If the number represented by String overflows according\r
80f3e34f 3924 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.\r
252d9457 3925 //\r
b0934ac4 3926\r
252d9457 3927 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
3928 return (EFI_DEVICE_ERROR);\r
3929 }\r
3930\r
3931 Result = MultU64x32(Result, 10) + (*String - L'0');\r
3932 String++;\r
3933\r
3934 //\r
3935 // Stop at spaces if requested\r
3936 //\r
3937 if (StopAtSpace && *String == L' ') {\r
3938 break;\r
3939 }\r
3940 }\r
3941\r
3942 *Value = Result;\r
b0934ac4 3943\r
252d9457 3944 return (EFI_SUCCESS);\r
3945}\r
3946\r
3947/**\r
3948 Function to verify and convert a string to its numerical value.\r
3949\r
3950 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
3951\r
3952 @param[in] String The string to evaluate.\r
3953 @param[out] Value Upon a successful return the value of the conversion.\r
3954 @param[in] ForceHex TRUE - always assume hex.\r
3955 @param[in] StopAtSpace FALSE to skip spaces.\r
b0934ac4 3956\r
252d9457 3957 @retval EFI_SUCCESS The conversion was successful.\r
3958 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
3959 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
3960**/\r
3961EFI_STATUS\r
3962EFIAPI\r
3963ShellConvertStringToUint64(\r
3964 IN CONST CHAR16 *String,\r
3965 OUT UINT64 *Value,\r
3966 IN CONST BOOLEAN ForceHex,\r
3967 IN CONST BOOLEAN StopAtSpace\r
3968 )\r
3969{\r
3970 UINT64 RetVal;\r
3971 CONST CHAR16 *Walker;\r
3972 EFI_STATUS Status;\r
3973 BOOLEAN Hex;\r
3974\r
3975 Hex = ForceHex;\r
3976\r
658bf43e 3977 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {\r
252d9457 3978 if (!Hex) {\r
3979 Hex = TRUE;\r
658bf43e 3980 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {\r
252d9457 3981 return (EFI_INVALID_PARAMETER);\r
3982 }\r
3983 } else {\r
3984 return (EFI_INVALID_PARAMETER);\r
3985 }\r
3986 }\r
3987\r
3988 //\r
3989 // Chop off leading spaces\r
3990 //\r
3991 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
3992\r
3993 //\r
3994 // make sure we have something left that is numeric.\r
3995 //\r
658bf43e 3996 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {\r
252d9457 3997 return (EFI_INVALID_PARAMETER);\r
b0934ac4 3998 }\r
252d9457 3999\r
4000 //\r
4001 // do the conversion.\r
4002 //\r
4003 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
4004 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
4005 } else {\r
4006 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
4007 }\r
4008\r
4009 if (Value == NULL && !EFI_ERROR(Status)) {\r
4010 return (EFI_NOT_FOUND);\r
4011 }\r
4012\r
4013 if (Value != NULL) {\r
4014 *Value = RetVal;\r
4015 }\r
4016\r
4017 return (Status);\r
4018}\r
4019\r
4020/**\r
4021 Function to determin if an entire string is a valid number.\r
4022\r
4023 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
4024\r
4025 @param[in] String The string to evaluate.\r
4026 @param[in] ForceHex TRUE - always assume hex.\r
4027 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
4028\r
4029 @retval TRUE It is all numeric (dec/hex) characters.\r
4030 @retval FALSE There is a non-numeric character.\r
4031**/\r
4032BOOLEAN\r
4033EFIAPI\r
4034ShellIsHexOrDecimalNumber (\r
4035 IN CONST CHAR16 *String,\r
4036 IN CONST BOOLEAN ForceHex,\r
4037 IN CONST BOOLEAN StopAtSpace\r
4038 )\r
4039{\r
4040 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
4041 return (TRUE);\r
4042 }\r
4043 return (FALSE);\r
4044}\r
4d0a4fce 4045\r
4046/**\r
4047 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
4048 buffer. The returned buffer must be callee freed.\r
4049\r
4050 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
4051 maintained and not changed for all operations with the same file.\r
4052\r
4ff7e37b
ED
4053 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
4054 @param[in, out] Ascii Boolean value for indicating whether the file is\r
4055 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 4056\r
beab0fc5 4057 @return The line of text from the file.\r
4058 @retval NULL There was not enough memory available.\r
4d0a4fce 4059\r
4060 @sa ShellFileHandleReadLine\r
4061**/\r
4062CHAR16*\r
4063EFIAPI\r
4064ShellFileHandleReturnLine(\r
4065 IN SHELL_FILE_HANDLE Handle,\r
4066 IN OUT BOOLEAN *Ascii\r
4067 )\r
4068{\r
4069 CHAR16 *RetVal;\r
4070 UINTN Size;\r
4071 EFI_STATUS Status;\r
4072\r
4073 Size = 0;\r
4074 RetVal = NULL;\r
4075\r
4076 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
4077 if (Status == EFI_BUFFER_TOO_SMALL) {\r
4078 RetVal = AllocateZeroPool(Size);\r
beab0fc5 4079 if (RetVal == NULL) {\r
4080 return (NULL);\r
4081 }\r
4d0a4fce 4082 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
b0934ac4 4083\r
4d0a4fce 4084 }\r
e3b76f3b 4085 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {\r
f79d7b62
QS
4086 Status = EFI_SUCCESS;\r
4087 }\r
4d0a4fce 4088 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
4089 FreePool(RetVal);\r
4090 RetVal = NULL;\r
4091 }\r
4092 return (RetVal);\r
4093}\r
4094\r
4095/**\r
4096 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
4097\r
4098 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
4099 maintained and not changed for all operations with the same file.\r
4100\r
2dda8a12
JD
4101 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ\r
4102 IS IN ASCII FORMAT.\r
4103\r
4ff7e37b 4104 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
2dda8a12
JD
4105 @param[in, out] Buffer The pointer to buffer to read into. If this function\r
4106 returns EFI_SUCCESS, then on output Buffer will\r
4107 contain a UCS2 string, even if the file being\r
4108 read is ASCII.\r
4109 @param[in, out] Size On input, pointer to number of bytes in Buffer.\r
4110 On output, unchanged unless Buffer is too small\r
4111 to contain the next line of the file. In that\r
4112 case Size is set to the number of bytes needed\r
4113 to hold the next line of the file (as a UCS2\r
4114 string, even if it is an ASCII file).\r
4ff7e37b
ED
4115 @param[in] Truncate If the buffer is large enough, this has no effect.\r
4116 If the buffer is is too small and Truncate is TRUE,\r
4117 the line will be truncated.\r
4118 If the buffer is is too small and Truncate is FALSE,\r
4119 then no read will occur.\r
4120\r
4121 @param[in, out] Ascii Boolean value for indicating whether the file is\r
4122 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 4123\r
4124 @retval EFI_SUCCESS The operation was successful. The line is stored in\r
4125 Buffer.\r
9ed21946 4126 @retval EFI_END_OF_FILE There are no more lines in the file.\r
4d0a4fce 4127 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
4128 @retval EFI_INVALID_PARAMETER Size was NULL.\r
4129 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
4130 Size was updated to the minimum space required.\r
4131**/\r
4132EFI_STATUS\r
4133EFIAPI\r
4134ShellFileHandleReadLine(\r
4135 IN SHELL_FILE_HANDLE Handle,\r
4136 IN OUT CHAR16 *Buffer,\r
4137 IN OUT UINTN *Size,\r
4138 IN BOOLEAN Truncate,\r
4139 IN OUT BOOLEAN *Ascii\r
4140 )\r
4141{\r
4142 EFI_STATUS Status;\r
4143 CHAR16 CharBuffer;\r
4144 UINTN CharSize;\r
4145 UINTN CountSoFar;\r
4146 UINT64 OriginalFilePosition;\r
4147\r
4148\r
4149 if (Handle == NULL\r
4150 ||Size == NULL\r
4151 ){\r
4152 return (EFI_INVALID_PARAMETER);\r
4153 }\r
4154 if (Buffer == NULL) {\r
4155 ASSERT(*Size == 0);\r
4156 } else {\r
4157 *Buffer = CHAR_NULL;\r
4158 }\r
4159 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
4160 if (OriginalFilePosition == 0) {\r
4161 CharSize = sizeof(CHAR16);\r
4162 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
4163 ASSERT_EFI_ERROR(Status);\r
4164 if (CharBuffer == gUnicodeFileTag) {\r
4165 *Ascii = FALSE;\r
4166 } else {\r
4167 *Ascii = TRUE;\r
4168 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
4169 }\r
4170 }\r
4171\r
9ed21946 4172 if (*Ascii) {\r
4173 CharSize = sizeof(CHAR8);\r
4174 } else {\r
4175 CharSize = sizeof(CHAR16);\r
4176 }\r
4d0a4fce 4177 for (CountSoFar = 0;;CountSoFar++){\r
4178 CharBuffer = 0;\r
4d0a4fce 4179 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
4180 if ( EFI_ERROR(Status)\r
4181 || CharSize == 0\r
4182 || (CharBuffer == L'\n' && !(*Ascii))\r
4183 || (CharBuffer == '\n' && *Ascii)\r
4184 ){\r
9ed21946 4185 if (CharSize == 0) {\r
4186 Status = EFI_END_OF_FILE;\r
4187 }\r
4d0a4fce 4188 break;\r
4189 }\r
4190 //\r
4191 // if we have space save it...\r
4192 //\r
1095b432 4193 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
4d0a4fce 4194 ASSERT(Buffer != NULL);\r
1095b432
JD
4195 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
4196 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
4d0a4fce 4197 }\r
4198 }\r
4199\r
4200 //\r
4201 // if we ran out of space tell when...\r
4202 //\r
1095b432
JD
4203 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
4204 *Size = (CountSoFar+1)*sizeof(CHAR16);\r
4205 if (!Truncate) {\r
4206 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
4207 } else {\r
4208 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
4d0a4fce 4209 }\r
1095b432
JD
4210 return (EFI_BUFFER_TOO_SMALL);\r
4211 }\r
4212 while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
4213 Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
4d0a4fce 4214 }\r
4215\r
4216 return (Status);\r
4217}\r
fb5278ef 4218\r
365aa98a 4219/**\r
4220 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.\r
4221\r
4222 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.\r
4223 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).\r
4224 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints \r
4225 the help content only.\r
4226 @retval EFI_DEVICE_ERROR The help data format was incorrect.\r
4227 @retval EFI_NOT_FOUND The help data could not be found.\r
4228 @retval EFI_SUCCESS The operation was successful.\r
4229**/\r
4230EFI_STATUS\r
4231EFIAPI\r
4232ShellPrintHelp (\r
4233 IN CONST CHAR16 *CommandToGetHelpOn,\r
4234 IN CONST CHAR16 *SectionToGetHelpOn,\r
4235 IN BOOLEAN PrintCommandText\r
4236 )\r
4237{\r
4238 EFI_STATUS Status;\r
4239 CHAR16 *OutText;\r
4240 \r
4241 OutText = NULL;\r
4242 \r
4243 //\r
4244 // Get the string to print based\r
4245 //\r
4246 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);\r
4247 \r
4248 //\r
4249 // make sure we got a valid string\r
4250 //\r
4251 if (EFI_ERROR(Status)){\r
4252 return Status;\r
4253 } \r
4254 if (OutText == NULL || StrLen(OutText) == 0) {\r
4255 return EFI_NOT_FOUND; \r
4256 }\r
4257 \r
4258 //\r
4259 // Chop off trailing stuff we dont need\r
4260 //\r
4261 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {\r
4262 OutText[StrLen(OutText)-1] = CHAR_NULL;\r
4263 }\r
4264 \r
4265 //\r
4266 // Print this out to the console\r
4267 //\r
4268 if (PrintCommandText) {\r
4269 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);\r
4270 } else {\r
4271 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);\r
4272 }\r
4273 \r
4274 SHELL_FREE_NON_NULL(OutText);\r
4275\r
4276 return EFI_SUCCESS;\r
4277}\r
4278\r
fb5278ef 4279/**\r
4280 Function to delete a file by name\r
4281 \r
4282 @param[in] FileName Pointer to file name to delete.\r
4283 \r
4284 @retval EFI_SUCCESS the file was deleted sucessfully\r
4285 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
4286 deleted\r
4287 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
4288 @retval EFI_NOT_FOUND The specified file could not be found on the\r
4289 device or the file system could not be found\r
4290 on the device.\r
4291 @retval EFI_NO_MEDIA The device has no medium.\r
4292 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
4293 medium is no longer supported.\r
4294 @retval EFI_DEVICE_ERROR The device reported an error.\r
4295 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
4296 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
4297 @retval EFI_ACCESS_DENIED The file was opened read only.\r
4298 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
4299 file.\r
4300 @retval other The file failed to open\r
4301**/\r
4302EFI_STATUS\r
4303EFIAPI\r
4304ShellDeleteFileByName(\r
4305 IN CONST CHAR16 *FileName\r
4306 )\r
4307{\r
4308 EFI_STATUS Status;\r
4309 SHELL_FILE_HANDLE FileHandle;\r
4310 \r
4311 Status = ShellFileExists(FileName);\r
4312 \r
4313 if (Status == EFI_SUCCESS){\r
4314 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);\r
4315 if (Status == EFI_SUCCESS){\r
4316 Status = ShellDeleteFile(&FileHandle);\r
4317 }\r
4318 } \r
4319\r
4320 return(Status);\r
4321 \r
4322}\r
0960ba17
QS
4323\r
4324/**\r
4325 Cleans off all the quotes in the string.\r
4326\r
4327 @param[in] OriginalString pointer to the string to be cleaned.\r
4328 @param[out] CleanString The new string with all quotes removed. \r
4329 Memory allocated in the function and free \r
4330 by caller.\r
4331\r
4332 @retval EFI_SUCCESS The operation was successful.\r
4333**/\r
4334EFI_STATUS\r
4335EFIAPI\r
4336InternalShellStripQuotes (\r
4337 IN CONST CHAR16 *OriginalString,\r
4338 OUT CHAR16 **CleanString\r
4339 )\r
4340{\r
4341 CHAR16 *Walker;\r
4342 \r
4343 if (OriginalString == NULL || CleanString == NULL) {\r
4344 return EFI_INVALID_PARAMETER;\r
4345 }\r
4346\r
4347 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);\r
4348 if (*CleanString == NULL) {\r
4349 return EFI_OUT_OF_RESOURCES;\r
4350 }\r
4351\r
4352 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
4353 if (*Walker == L'\"') {\r
4354 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
4355 }\r
4356 }\r
4357\r
4358 return EFI_SUCCESS;\r
4359}\r
4360\r