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