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