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