]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
Refine code to follow coding style.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
CommitLineData
94b17fa1 1/** @file\r
2 Provides interface to shell functionality for shell commands and applications.\r
3\r
49bd498d 4 Copyright (c) 2006 - 2012, 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
b0934ac4 166 Function to do most of the work of the constructor. Allows for calling\r
a405b86d 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
b0934ac4 424 @retval EFI_SUCCESS The information was set.\r
a405b86d 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
b0934ac4 427 @retval EFI_NO_MEDIA The device has no medium.\r
428 @retval EFI_DEVICE_ERROR The device reported an error.\r
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
a405b86d 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
b0934ac4 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
b0934ac4 450 @param FilePath on input the device path to the file. On output\r
94b17fa1 451 the remaining device path.\r
b0934ac4 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
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
1e6e84c7 461 device or the file system could not be found on\r
94b17fa1 462 the device.\r
b0934ac4 463 @retval EFI_NO_MEDIA The device has no medium.\r
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 465 medium is no longer supported.\r
b0934ac4 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
469 @retval EFI_ACCESS_DENIED The file was opened read only.\r
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 471 file.\r
b0934ac4 472 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 473**/\r
474EFI_STATUS\r
475EFIAPI\r
476ShellOpenFileByDevicePath(\r
b0934ac4 477 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
478 OUT EFI_HANDLE *DeviceHandle,\r
a405b86d 479 OUT SHELL_FILE_HANDLE *FileHandle,\r
b0934ac4 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
b0934ac4 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
94b17fa1 612\r
b0934ac4 613 @retval EFI_SUCCESS The information was set.\r
614 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
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
1e6e84c7 617 device or the file system could not be found\r
94b17fa1 618 on the device.\r
b0934ac4 619 @retval EFI_NO_MEDIA The device has no medium.\r
620 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 621 medium is no longer supported.\r
b0934ac4 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
626 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 627 file.\r
b0934ac4 628 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 629**/\r
630EFI_STATUS\r
631EFIAPI\r
632ShellOpenFileByName(\r
b0934ac4 633 IN CONST CHAR16 *FileName,\r
a405b86d 634 OUT SHELL_FILE_HANDLE *FileHandle,\r
94b17fa1 635 IN UINT64 OpenMode,\r
b0934ac4 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
b0934ac4 695 @param DirectoryName pointer to directory name\r
696 @param FileHandle pointer to the file handle.\r
94b17fa1 697\r
b0934ac4 698 @retval EFI_SUCCESS The information was set.\r
699 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
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
1e6e84c7 702 device or the file system could not be found\r
94b17fa1 703 on the device.\r
b0934ac4 704 @retval EFI_NO_MEDIA The device has no medium.\r
705 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 706 medium is no longer supported.\r
b0934ac4 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
711 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 712 file.\r
b0934ac4 713 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 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
b0934ac4 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
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
b0934ac4 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
94b17fa1 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
b0934ac4 848 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
94b17fa1 849**/\r
850EFI_STATUS\r
851EFIAPI\r
852ShellDeleteFile (\r
b0934ac4 853 IN SHELL_FILE_HANDLE *FileHandle\r
a405b86d 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
b0934ac4 881 IN SHELL_FILE_HANDLE FileHandle,\r
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
b0934ac4 935/** Retrieve first entry from a directory.\r
94b17fa1 936\r
b0934ac4 937 This function takes an open directory handle and gets information from the\r
938 first entry in the directory. A buffer is allocated to contain\r
939 the information and a pointer to the buffer is returned in *Buffer. The\r
940 caller can use ShellFindNextFile() to get subsequent directory entries.\r
94b17fa1 941\r
b0934ac4 942 The buffer will be freed by ShellFindNextFile() when the last directory\r
943 entry is read. Otherwise, the caller must free the buffer, using FreePool,\r
944 when finished with it.\r
945\r
946 @param[in] DirHandle The file handle of the directory to search.\r
947 @param[out] Buffer The pointer to the buffer for the file's information.\r
94b17fa1 948\r
949 @retval EFI_SUCCESS Found the first file.\r
950 @retval EFI_NOT_FOUND Cannot find the directory.\r
951 @retval EFI_NO_MEDIA The device has no media.\r
952 @retval EFI_DEVICE_ERROR The device reported an error.\r
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
954 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
955 or ShellReadFile\r
956**/\r
957EFI_STATUS\r
958EFIAPI\r
959ShellFindFirstFile (\r
a405b86d 960 IN SHELL_FILE_HANDLE DirHandle,\r
d2b4564b 961 OUT EFI_FILE_INFO **Buffer\r
a405b86d 962 )\r
963{\r
94b17fa1 964 //\r
d2b4564b 965 // pass to file handle lib\r
94b17fa1 966 //\r
d2b4564b 967 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
94b17fa1 968}\r
b0934ac4 969/** Retrieve next entries from a directory.\r
94b17fa1 970\r
b0934ac4 971 To use this function, the caller must first call the ShellFindFirstFile()\r
972 function to get the first directory entry. Subsequent directory entries are\r
973 retrieved by using the ShellFindNextFile() function. This function can\r
974 be called several times to get each entry from the directory. If the call of\r
975 ShellFindNextFile() retrieved the last directory entry, the next call of\r
976 this function will set *NoFile to TRUE and free the buffer.\r
94b17fa1 977\r
b0934ac4 978 @param[in] DirHandle The file handle of the directory.\r
979 @param[out] Buffer The pointer to buffer for file's information.\r
980 @param[out] NoFile The pointer to boolean when last file is found.\r
94b17fa1 981\r
982 @retval EFI_SUCCESS Found the next file, or reached last file\r
983 @retval EFI_NO_MEDIA The device has no media.\r
984 @retval EFI_DEVICE_ERROR The device reported an error.\r
985 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
986**/\r
987EFI_STATUS\r
988EFIAPI\r
989ShellFindNextFile(\r
a405b86d 990 IN SHELL_FILE_HANDLE DirHandle,\r
94b17fa1 991 OUT EFI_FILE_INFO *Buffer,\r
992 OUT BOOLEAN *NoFile\r
a405b86d 993 )\r
994{\r
94b17fa1 995 //\r
d2b4564b 996 // pass to file handle lib\r
94b17fa1 997 //\r
d2b4564b 998 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
94b17fa1 999}\r
1000/**\r
1001 Retrieve the size of a file.\r
1002\r
1003 if FileHandle is NULL then ASSERT()\r
1004 if Size is NULL then ASSERT()\r
1005\r
1e6e84c7 1006 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
94b17fa1 1007 data.\r
1008\r
1009 @param FileHandle file handle from which size is retrieved\r
1010 @param Size pointer to size\r
1011\r
1012 @retval EFI_SUCCESS operation was completed sucessfully\r
1013 @retval EFI_DEVICE_ERROR cannot access the file\r
1014**/\r
1015EFI_STATUS\r
1016EFIAPI\r
1017ShellGetFileSize (\r
a405b86d 1018 IN SHELL_FILE_HANDLE FileHandle,\r
94b17fa1 1019 OUT UINT64 *Size\r
a405b86d 1020 )\r
1021{\r
d2b4564b 1022 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
94b17fa1 1023}\r
1024/**\r
1025 Retrieves the status of the break execution flag\r
1026\r
1027 this function is useful to check whether the application is being asked to halt by the shell.\r
1028\r
1029 @retval TRUE the execution break is enabled\r
1030 @retval FALSE the execution break is not enabled\r
1031**/\r
1032BOOLEAN\r
1033EFIAPI\r
1034ShellGetExecutionBreakFlag(\r
1035 VOID\r
1036 )\r
1037{\r
1e6e84c7 1038 //\r
94b17fa1 1039 // Check for UEFI Shell 2.0 protocols\r
1040 //\r
366f81a0 1041 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1042\r
1043 //\r
1044 // We are using UEFI Shell 2.0; see if the event has been triggered\r
1045 //\r
366f81a0 1046 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
94b17fa1 1047 return (FALSE);\r
1048 }\r
1049 return (TRUE);\r
1e6e84c7 1050 }\r
94b17fa1 1051\r
1052 //\r
1053 // using EFI Shell; call the function to check\r
1054 //\r
92a5447e 1055 if (mEfiShellEnvironment2 != NULL) {\r
1056 return (mEfiShellEnvironment2->GetExecutionBreak());\r
1057 }\r
1058\r
1059 return (FALSE);\r
94b17fa1 1060}\r
1061/**\r
1062 return the value of an environment variable\r
1063\r
1e6e84c7 1064 this function gets the value of the environment variable set by the\r
94b17fa1 1065 ShellSetEnvironmentVariable function\r
1066\r
1067 @param EnvKey The key name of the environment variable.\r
1068\r
1069 @retval NULL the named environment variable does not exist.\r
1070 @return != NULL pointer to the value of the environment variable\r
1071**/\r
1072CONST CHAR16*\r
1073EFIAPI\r
1074ShellGetEnvironmentVariable (\r
9b3bf083 1075 IN CONST CHAR16 *EnvKey\r
94b17fa1 1076 )\r
1077{\r
1e6e84c7 1078 //\r
94b17fa1 1079 // Check for UEFI Shell 2.0 protocols\r
1080 //\r
366f81a0 1081 if (gEfiShellProtocol != NULL) {\r
1082 return (gEfiShellProtocol->GetEnv(EnvKey));\r
94b17fa1 1083 }\r
1084\r
1085 //\r
92a5447e 1086 // Check for EFI shell\r
94b17fa1 1087 //\r
92a5447e 1088 if (mEfiShellEnvironment2 != NULL) {\r
1089 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
1090 }\r
94b17fa1 1091\r
92a5447e 1092 return NULL;\r
94b17fa1 1093}\r
1094/**\r
1095 set the value of an environment variable\r
1096\r
1097This function changes the current value of the specified environment variable. If the\r
1098environment variable exists and the Value is an empty string, then the environment\r
1099variable is deleted. If the environment variable exists and the Value is not an empty\r
1100string, then the value of the environment variable is changed. If the environment\r
1101variable does not exist and the Value is an empty string, there is no action. If the\r
1102environment variable does not exist and the Value is a non-empty string, then the\r
1103environment variable is created and assigned the specified value.\r
1104\r
1105 This is not supported pre-UEFI Shell 2.0.\r
1106\r
1107 @param EnvKey The key name of the environment variable.\r
1108 @param EnvVal The Value of the environment variable\r
1109 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1110\r
1111 @retval EFI_SUCCESS the operation was completed sucessfully\r
1112 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1113**/\r
1114EFI_STATUS\r
1115EFIAPI\r
1116ShellSetEnvironmentVariable (\r
1117 IN CONST CHAR16 *EnvKey,\r
1118 IN CONST CHAR16 *EnvVal,\r
1119 IN BOOLEAN Volatile\r
1120 )\r
1121{\r
1e6e84c7 1122 //\r
94b17fa1 1123 // Check for UEFI Shell 2.0 protocols\r
1124 //\r
366f81a0 1125 if (gEfiShellProtocol != NULL) {\r
1126 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
1e6e84c7 1127 }\r
94b17fa1 1128\r
1129 //\r
1130 // This feature does not exist under EFI shell\r
1131 //\r
1132 return (EFI_UNSUPPORTED);\r
1133}\r
a405b86d 1134\r
94b17fa1 1135/**\r
a405b86d 1136 Cause the shell to parse and execute a command line.\r
94b17fa1 1137\r
1138 This function creates a nested instance of the shell and executes the specified\r
a405b86d 1139 command (CommandLine) with the specified environment (Environment). Upon return,\r
1140 the status code returned by the specified command is placed in StatusCode.\r
1141 If Environment is NULL, then the current environment is used and all changes made\r
1142 by the commands executed will be reflected in the current environment. If the\r
1143 Environment is non-NULL, then the changes made will be discarded.\r
1144 The CommandLine is executed from the current working directory on the current\r
1145 device.\r
1146\r
1147 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0\r
1148 environment. The values pointed to by the parameters will be unchanged by the\r
1149 ShellExecute() function. The Output parameter has no effect in a\r
1150 UEFI Shell 2.0 environment.\r
1151\r
1152 @param[in] ParentHandle The parent image starting the operation.\r
1153 @param[in] CommandLine The pointer to a NULL terminated command line.\r
1154 @param[in] Output True to display debug output. False to hide it.\r
1155 @param[in] EnvironmentVariables Optional pointer to array of environment variables\r
1156 in the form "x=y". If NULL, the current set is used.\r
1157 @param[out] Status The status of the run command line.\r
1158\r
1159 @retval EFI_SUCCESS The operation completed sucessfully. Status\r
1160 contains the status code returned.\r
1161 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.\r
1162 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1163 @retval EFI_UNSUPPORTED The operation is not allowed.\r
94b17fa1 1164**/\r
1165EFI_STATUS\r
1166EFIAPI\r
1167ShellExecute (\r
1168 IN EFI_HANDLE *ParentHandle,\r
1169 IN CHAR16 *CommandLine OPTIONAL,\r
1170 IN BOOLEAN Output OPTIONAL,\r
1171 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1172 OUT EFI_STATUS *Status OPTIONAL\r
1173 )\r
1174{\r
1e6e84c7 1175 //\r
94b17fa1 1176 // Check for UEFI Shell 2.0 protocols\r
1177 //\r
366f81a0 1178 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1179 //\r
1180 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1181 //\r
366f81a0 1182 return (gEfiShellProtocol->Execute(ParentHandle,\r
94b17fa1 1183 CommandLine,\r
1184 EnvironmentVariables,\r
1185 Status));\r
1e6e84c7 1186 }\r
92a5447e 1187\r
94b17fa1 1188 //\r
92a5447e 1189 // Check for EFI shell\r
94b17fa1 1190 //\r
92a5447e 1191 if (mEfiShellEnvironment2 != NULL) {\r
1192 //\r
1193 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
1194 // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
1195 //\r
1196 return (mEfiShellEnvironment2->Execute(*ParentHandle,\r
1197 CommandLine,\r
1198 Output));\r
1199 }\r
1200\r
1201 return (EFI_UNSUPPORTED);\r
94b17fa1 1202}\r
1203/**\r
1204 Retreives the current directory path\r
1205\r
1e6e84c7 1206 If the DeviceName is NULL, it returns the current device's current directory\r
1207 name. If the DeviceName is not NULL, it returns the current directory name\r
94b17fa1 1208 on specified drive.\r
1209\r
1210 @param DeviceName the name of the drive to get directory on\r
1211\r
1212 @retval NULL the directory does not exist\r
1213 @return != NULL the directory\r
1214**/\r
1215CONST CHAR16*\r
1216EFIAPI\r
1217ShellGetCurrentDir (\r
a405b86d 1218 IN CHAR16 * CONST DeviceName OPTIONAL\r
94b17fa1 1219 )\r
1220{\r
1e6e84c7 1221 //\r
94b17fa1 1222 // Check for UEFI Shell 2.0 protocols\r
1223 //\r
366f81a0 1224 if (gEfiShellProtocol != NULL) {\r
1225 return (gEfiShellProtocol->GetCurDir(DeviceName));\r
1e6e84c7 1226 }\r
92a5447e 1227\r
94b17fa1 1228 //\r
8bd282be 1229 // Check for EFI shell\r
94b17fa1 1230 //\r
8bd282be 1231 if (mEfiShellEnvironment2 != NULL) {\r
1232 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1233 }\r
1234\r
1235 return (NULL);\r
94b17fa1 1236}\r
1237/**\r
1238 sets (enabled or disabled) the page break mode\r
1239\r
1e6e84c7 1240 when page break mode is enabled the screen will stop scrolling\r
94b17fa1 1241 and wait for operator input before scrolling a subsequent screen.\r
1242\r
1243 @param CurrentState TRUE to enable and FALSE to disable\r
1244**/\r
1e6e84c7 1245VOID\r
94b17fa1 1246EFIAPI\r
1247ShellSetPageBreakMode (\r
1248 IN BOOLEAN CurrentState\r
1249 )\r
1250{\r
1251 //\r
1252 // check for enabling\r
1253 //\r
1254 if (CurrentState != 0x00) {\r
1e6e84c7 1255 //\r
94b17fa1 1256 // check for UEFI Shell 2.0\r
1257 //\r
366f81a0 1258 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1259 //\r
1260 // Enable with UEFI 2.0 Shell\r
1261 //\r
366f81a0 1262 gEfiShellProtocol->EnablePageBreak();\r
94b17fa1 1263 return;\r
1264 } else {\r
1e6e84c7 1265 //\r
92a5447e 1266 // Check for EFI shell\r
94b17fa1 1267 //\r
92a5447e 1268 if (mEfiShellEnvironment2 != NULL) {\r
1269 //\r
1270 // Enable with EFI Shell\r
1271 //\r
1272 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1273 return;\r
1274 }\r
94b17fa1 1275 }\r
1276 } else {\r
1e6e84c7 1277 //\r
94b17fa1 1278 // check for UEFI Shell 2.0\r
1279 //\r
366f81a0 1280 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1281 //\r
1282 // Disable with UEFI 2.0 Shell\r
1283 //\r
366f81a0 1284 gEfiShellProtocol->DisablePageBreak();\r
94b17fa1 1285 return;\r
1286 } else {\r
1e6e84c7 1287 //\r
92a5447e 1288 // Check for EFI shell\r
94b17fa1 1289 //\r
92a5447e 1290 if (mEfiShellEnvironment2 != NULL) {\r
1291 //\r
1292 // Disable with EFI Shell\r
1293 //\r
1294 mEfiShellEnvironment2->DisablePageBreak ();\r
1295 return;\r
1296 }\r
94b17fa1 1297 }\r
1298 }\r
1299}\r
1300\r
1301///\r
1302/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1303/// This allows for the struct to be populated.\r
1304///\r
1305typedef struct {\r
d2b4564b 1306 LIST_ENTRY Link;\r
94b17fa1 1307 EFI_STATUS Status;\r
1308 CHAR16 *FullName;\r
1309 CHAR16 *FileName;\r
a405b86d 1310 SHELL_FILE_HANDLE Handle;\r
94b17fa1 1311 EFI_FILE_INFO *Info;\r
1312} EFI_SHELL_FILE_INFO_NO_CONST;\r
1313\r
1314/**\r
1315 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1316\r
1317 if OldStyleFileList is NULL then ASSERT()\r
1318\r
1e6e84c7 1319 this function will convert a SHELL_FILE_ARG based list into a callee allocated\r
94b17fa1 1320 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1321 the ShellCloseFileMetaArg function.\r
1322\r
4ff7e37b
ED
1323 @param[in] FileList the EFI shell list type\r
1324 @param[in, out] ListHead the list to add to\r
94b17fa1 1325\r
1326 @retval the resultant head of the double linked new format list;\r
1327**/\r
1328LIST_ENTRY*\r
1329EFIAPI\r
1330InternalShellConvertFileListType (\r
9b3bf083 1331 IN LIST_ENTRY *FileList,\r
1332 IN OUT LIST_ENTRY *ListHead\r
125c2cf4 1333 )\r
1334{\r
94b17fa1 1335 SHELL_FILE_ARG *OldInfo;\r
9b3bf083 1336 LIST_ENTRY *Link;\r
94b17fa1 1337 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1338\r
1339 //\r
9b3bf083 1340 // ASSERTs\r
94b17fa1 1341 //\r
9b3bf083 1342 ASSERT(FileList != NULL);\r
1343 ASSERT(ListHead != NULL);\r
94b17fa1 1344\r
1345 //\r
1346 // enumerate through each member of the old list and copy\r
1347 //\r
d2b4564b 1348 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
94b17fa1 1349 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
a405b86d 1350 ASSERT(OldInfo != NULL);\r
1351\r
1352 //\r
1353 // Skip ones that failed to open...\r
1354 //\r
1355 if (OldInfo->Status != EFI_SUCCESS) {\r
1356 continue;\r
1357 }\r
94b17fa1 1358\r
1359 //\r
1360 // make sure the old list was valid\r
1361 //\r
94b17fa1 1362 ASSERT(OldInfo->Info != NULL);\r
1363 ASSERT(OldInfo->FullName != NULL);\r
1364 ASSERT(OldInfo->FileName != NULL);\r
1365\r
1366 //\r
1367 // allocate a new EFI_SHELL_FILE_INFO object\r
1368 //\r
1369 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
c9d92df0 1370 if (NewInfo == NULL) {\r
7a95efda 1371 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));\r
beab0fc5 1372 ListHead = NULL;\r
c9d92df0 1373 break;\r
1374 }\r
1e6e84c7 1375\r
1376 //\r
94b17fa1 1377 // copy the simple items\r
1378 //\r
1379 NewInfo->Handle = OldInfo->Handle;\r
1380 NewInfo->Status = OldInfo->Status;\r
1381\r
d2b4564b 1382 // old shell checks for 0 not NULL\r
1383 OldInfo->Handle = 0;\r
1384\r
94b17fa1 1385 //\r
1386 // allocate new space to copy strings and structure\r
1387 //\r
1388 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r
1389 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r
1390 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r
1e6e84c7 1391\r
94b17fa1 1392 //\r
1393 // make sure all the memory allocations were sucessful\r
1394 //\r
beab0fc5 1395 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {\r
7a95efda 1396 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));\r
beab0fc5 1397 ListHead = NULL;\r
1398 break;\r
1399 }\r
94b17fa1 1400\r
1401 //\r
1402 // Copt the strings and structure\r
1403 //\r
1404 StrCpy(NewInfo->FullName, OldInfo->FullName);\r
1405 StrCpy(NewInfo->FileName, OldInfo->FileName);\r
1406 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r
1407\r
1408 //\r
1409 // add that to the list\r
1410 //\r
9b3bf083 1411 InsertTailList(ListHead, &NewInfo->Link);\r
94b17fa1 1412 }\r
1413 return (ListHead);\r
1414}\r
1415/**\r
1416 Opens a group of files based on a path.\r
1417\r
1e6e84c7 1418 This function uses the Arg to open all the matching files. Each matched\r
1419 file has a SHELL_FILE_ARG structure to record the file information. These\r
1420 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG\r
94b17fa1 1421 structures from ListHead to access each file. This function supports wildcards\r
1e6e84c7 1422 and will process '?' and '*' as such. the list must be freed with a call to\r
94b17fa1 1423 ShellCloseFileMetaArg().\r
1424\r
1e6e84c7 1425 If you are NOT appending to an existing list *ListHead must be NULL. If\r
5f7431d0 1426 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1427\r
1428 @param Arg pointer to path string\r
1429 @param OpenMode mode to open files with\r
1430 @param ListHead head of linked list of results\r
1431\r
1e6e84c7 1432 @retval EFI_SUCCESS the operation was sucessful and the list head\r
94b17fa1 1433 contains the list of opened files\r
94b17fa1 1434 @return != EFI_SUCCESS the operation failed\r
1435\r
1436 @sa InternalShellConvertFileListType\r
1437**/\r
1438EFI_STATUS\r
1439EFIAPI\r
1440ShellOpenFileMetaArg (\r
1441 IN CHAR16 *Arg,\r
1442 IN UINT64 OpenMode,\r
1443 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1444 )\r
1445{\r
1446 EFI_STATUS Status;\r
9b3bf083 1447 LIST_ENTRY mOldStyleFileList;\r
1e6e84c7 1448\r
94b17fa1 1449 //\r
1450 // ASSERT that Arg and ListHead are not NULL\r
1451 //\r
1452 ASSERT(Arg != NULL);\r
1453 ASSERT(ListHead != NULL);\r
1454\r
1e6e84c7 1455 //\r
94b17fa1 1456 // Check for UEFI Shell 2.0 protocols\r
1457 //\r
366f81a0 1458 if (gEfiShellProtocol != NULL) {\r
5f7431d0 1459 if (*ListHead == NULL) {\r
1460 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1461 if (*ListHead == NULL) {\r
1462 return (EFI_OUT_OF_RESOURCES);\r
1463 }\r
1464 InitializeListHead(&((*ListHead)->Link));\r
1e6e84c7 1465 }\r
366f81a0 1466 Status = gEfiShellProtocol->OpenFileList(Arg,\r
1e6e84c7 1467 OpenMode,\r
2247dde4 1468 ListHead);\r
1469 if (EFI_ERROR(Status)) {\r
366f81a0 1470 gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
2247dde4 1471 } else {\r
366f81a0 1472 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
2247dde4 1473 }\r
a405b86d 1474 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {\r
1475 FreePool(*ListHead);\r
1476 *ListHead = NULL;\r
1477 return (EFI_NOT_FOUND);\r
1478 }\r
2247dde4 1479 return (Status);\r
1e6e84c7 1480 }\r
94b17fa1 1481\r
1482 //\r
92a5447e 1483 // Check for EFI shell\r
94b17fa1 1484 //\r
92a5447e 1485 if (mEfiShellEnvironment2 != NULL) {\r
1486 //\r
1487 // make sure the list head is initialized\r
1488 //\r
1489 InitializeListHead(&mOldStyleFileList);\r
94b17fa1 1490\r
92a5447e 1491 //\r
1492 // Get the EFI Shell list of files\r
1493 //\r
1494 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
1495 if (EFI_ERROR(Status)) {\r
1496 *ListHead = NULL;\r
1497 return (Status);\r
1498 }\r
94b17fa1 1499\r
9b3bf083 1500 if (*ListHead == NULL) {\r
92a5447e 1501 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1502 if (*ListHead == NULL) {\r
1503 return (EFI_OUT_OF_RESOURCES);\r
1504 }\r
1505 InitializeListHead(&((*ListHead)->Link));\r
9b3bf083 1506 }\r
9b3bf083 1507\r
92a5447e 1508 //\r
1509 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1510 //\r
1511 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1512\r
92a5447e 1513 //\r
1514 // Free the EFI Shell version that was converted.\r
1515 //\r
1516 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
94b17fa1 1517\r
92a5447e 1518 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
1519 FreePool(*ListHead);\r
1520 *ListHead = NULL;\r
1521 Status = EFI_NOT_FOUND;\r
1522 }\r
1523 return (Status);\r
a405b86d 1524 }\r
1525\r
92a5447e 1526 return (EFI_UNSUPPORTED);\r
94b17fa1 1527}\r
1528/**\r
a405b86d 1529 Free the linked list returned from ShellOpenFileMetaArg.\r
94b17fa1 1530\r
a405b86d 1531 if ListHead is NULL then ASSERT().\r
94b17fa1 1532\r
a405b86d 1533 @param ListHead the pointer to free.\r
94b17fa1 1534\r
a405b86d 1535 @retval EFI_SUCCESS the operation was sucessful.\r
94b17fa1 1536**/\r
1537EFI_STATUS\r
1538EFIAPI\r
1539ShellCloseFileMetaArg (\r
1540 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1541 )\r
1542{\r
1543 LIST_ENTRY *Node;\r
1544\r
1545 //\r
1546 // ASSERT that ListHead is not NULL\r
1547 //\r
1548 ASSERT(ListHead != NULL);\r
1549\r
1e6e84c7 1550 //\r
94b17fa1 1551 // Check for UEFI Shell 2.0 protocols\r
1552 //\r
366f81a0 1553 if (gEfiShellProtocol != NULL) {\r
1554 return (gEfiShellProtocol->FreeFileList(ListHead));\r
92a5447e 1555 } else if (mEfiShellEnvironment2 != NULL) {\r
94b17fa1 1556 //\r
1e6e84c7 1557 // Since this is EFI Shell version we need to free our internally made copy\r
94b17fa1 1558 // of the list\r
1559 //\r
1e6e84c7 1560 for ( Node = GetFirstNode(&(*ListHead)->Link)\r
a405b86d 1561 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)\r
9b3bf083 1562 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
94b17fa1 1563 RemoveEntryList(Node);\r
a405b86d 1564 ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
94b17fa1 1565 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1566 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1567 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1568 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1569 }\r
1570 return EFI_SUCCESS;\r
1571 }\r
92a5447e 1572\r
1573 return (EFI_UNSUPPORTED);\r
94b17fa1 1574}\r
1575\r
125c2cf4 1576/**\r
1577 Find a file by searching the CWD and then the path.\r
1578\r
b3011f40 1579 If FileName is NULL then ASSERT.\r
125c2cf4 1580\r
b3011f40 1581 If the return value is not NULL then the memory must be caller freed.\r
125c2cf4 1582\r
1583 @param FileName Filename string.\r
1584\r
1585 @retval NULL the file was not found\r
1586 @return !NULL the full path to the file.\r
1587**/\r
1588CHAR16 *\r
1589EFIAPI\r
1590ShellFindFilePath (\r
1591 IN CONST CHAR16 *FileName\r
1592 )\r
1593{\r
1594 CONST CHAR16 *Path;\r
a405b86d 1595 SHELL_FILE_HANDLE Handle;\r
125c2cf4 1596 EFI_STATUS Status;\r
1597 CHAR16 *RetVal;\r
1598 CHAR16 *TestPath;\r
1599 CONST CHAR16 *Walker;\r
36a9d672 1600 UINTN Size;\r
1cd45e78 1601 CHAR16 *TempChar;\r
125c2cf4 1602\r
1603 RetVal = NULL;\r
1604\r
a405b86d 1605 //\r
1606 // First make sure its not an absolute path.\r
1607 //\r
1608 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);\r
1609 if (!EFI_ERROR(Status)){\r
1610 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1611 ASSERT(RetVal == NULL);\r
1612 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);\r
1613 ShellCloseFile(&Handle);\r
1614 return (RetVal);\r
1615 } else {\r
1616 ShellCloseFile(&Handle);\r
1617 }\r
1618 }\r
1619\r
125c2cf4 1620 Path = ShellGetEnvironmentVariable(L"cwd");\r
1621 if (Path != NULL) {\r
36a9d672 1622 Size = StrSize(Path);\r
1623 Size += StrSize(FileName);\r
1624 TestPath = AllocateZeroPool(Size);\r
c9d92df0 1625 if (TestPath == NULL) {\r
1626 return (NULL);\r
1627 }\r
125c2cf4 1628 StrCpy(TestPath, Path);\r
1629 StrCat(TestPath, FileName);\r
1630 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1631 if (!EFI_ERROR(Status)){\r
a405b86d 1632 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1633 ASSERT(RetVal == NULL);\r
1634 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1635 ShellCloseFile(&Handle);\r
1636 FreePool(TestPath);\r
1637 return (RetVal);\r
1638 } else {\r
1639 ShellCloseFile(&Handle);\r
1640 }\r
125c2cf4 1641 }\r
1642 FreePool(TestPath);\r
1643 }\r
1644 Path = ShellGetEnvironmentVariable(L"path");\r
1645 if (Path != NULL) {\r
a405b86d 1646 Size = StrSize(Path)+sizeof(CHAR16);\r
36a9d672 1647 Size += StrSize(FileName);\r
1648 TestPath = AllocateZeroPool(Size);\r
3e082d58 1649 if (TestPath == NULL) {\r
1650 return (NULL);\r
1651 }\r
1e6e84c7 1652 Walker = (CHAR16*)Path;\r
125c2cf4 1653 do {\r
1654 CopyMem(TestPath, Walker, StrSize(Walker));\r
3e082d58 1655 if (TestPath != NULL) {\r
1656 TempChar = StrStr(TestPath, L";");\r
1657 if (TempChar != NULL) {\r
1658 *TempChar = CHAR_NULL;\r
1659 }\r
1660 if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
1661 StrCat(TestPath, L"\\");\r
1662 }\r
89e8537a 1663 if (FileName[0] == L'\\') {\r
1664 FileName++;\r
1665 }\r
3e082d58 1666 StrCat(TestPath, FileName);\r
1667 if (StrStr(Walker, L";") != NULL) {\r
1668 Walker = StrStr(Walker, L";") + 1;\r
a405b86d 1669 } else {\r
3e082d58 1670 Walker = NULL;\r
1671 }\r
1672 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1673 if (!EFI_ERROR(Status)){\r
1674 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1675 ASSERT(RetVal == NULL);\r
1676 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1677 ShellCloseFile(&Handle);\r
1678 break;\r
1679 } else {\r
1680 ShellCloseFile(&Handle);\r
1681 }\r
a405b86d 1682 }\r
125c2cf4 1683 }\r
1684 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1685 FreePool(TestPath);\r
1686 }\r
1687 return (RetVal);\r
1688}\r
1689\r
b3011f40 1690/**\r
1e6e84c7 1691 Find a file by searching the CWD and then the path with a variable set of file\r
1692 extensions. If the file is not found it will append each extension in the list\r
b3011f40 1693 in the order provided and return the first one that is successful.\r
1694\r
1695 If FileName is NULL, then ASSERT.\r
1696 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1697\r
1698 If the return value is not NULL then the memory must be caller freed.\r
1699\r
1700 @param[in] FileName Filename string.\r
1701 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1702\r
1703 @retval NULL The file was not found.\r
1704 @retval !NULL The path to the file.\r
1705**/\r
1706CHAR16 *\r
1707EFIAPI\r
1708ShellFindFilePathEx (\r
1709 IN CONST CHAR16 *FileName,\r
1710 IN CONST CHAR16 *FileExtension\r
1711 )\r
1712{\r
1713 CHAR16 *TestPath;\r
1714 CHAR16 *RetVal;\r
1715 CONST CHAR16 *ExtensionWalker;\r
9e926b69 1716 UINTN Size;\r
1cd45e78 1717 CHAR16 *TempChar;\r
c9d92df0 1718 CHAR16 *TempChar2;\r
1cd45e78 1719\r
b3011f40 1720 ASSERT(FileName != NULL);\r
1721 if (FileExtension == NULL) {\r
1722 return (ShellFindFilePath(FileName));\r
1723 }\r
1724 RetVal = ShellFindFilePath(FileName);\r
1725 if (RetVal != NULL) {\r
1726 return (RetVal);\r
1727 }\r
9e926b69 1728 Size = StrSize(FileName);\r
1729 Size += StrSize(FileExtension);\r
1730 TestPath = AllocateZeroPool(Size);\r
c9d92df0 1731 if (TestPath == NULL) {\r
1732 return (NULL);\r
1733 }\r
a405b86d 1734 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){\r
b3011f40 1735 StrCpy(TestPath, FileName);\r
a405b86d 1736 if (ExtensionWalker != NULL) {\r
1737 StrCat(TestPath, ExtensionWalker);\r
1738 }\r
1cd45e78 1739 TempChar = StrStr(TestPath, L";");\r
1740 if (TempChar != NULL) {\r
1741 *TempChar = CHAR_NULL;\r
b3011f40 1742 }\r
1743 RetVal = ShellFindFilePath(TestPath);\r
1744 if (RetVal != NULL) {\r
1745 break;\r
1746 }\r
a405b86d 1747 ASSERT(ExtensionWalker != NULL);\r
c9d92df0 1748 TempChar2 = StrStr(ExtensionWalker, L";");\r
b3011f40 1749 }\r
1750 FreePool(TestPath);\r
1751 return (RetVal);\r
1752}\r
1753\r
94b17fa1 1754typedef struct {\r
9b3bf083 1755 LIST_ENTRY Link;\r
94b17fa1 1756 CHAR16 *Name;\r
a405b86d 1757 SHELL_PARAM_TYPE Type;\r
94b17fa1 1758 CHAR16 *Value;\r
1759 UINTN OriginalPosition;\r
1760} SHELL_PARAM_PACKAGE;\r
1761\r
1762/**\r
1e6e84c7 1763 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
94b17fa1 1764 return value is TRUE then the type parameter is set also.\r
1e6e84c7 1765\r
94b17fa1 1766 if CheckList is NULL then ASSERT();\r
1767 if Name is NULL then ASSERT();\r
1768 if Type is NULL then ASSERT();\r
1769\r
94b17fa1 1770 @param Name pointer to Name of parameter found\r
1771 @param CheckList List to check against\r
a405b86d 1772 @param Type pointer to type of parameter if it was found\r
94b17fa1 1773\r
1774 @retval TRUE the Parameter was found. Type is valid.\r
1775 @retval FALSE the Parameter was not found. Type is not valid.\r
1776**/\r
1777BOOLEAN\r
1778EFIAPI\r
d2b4564b 1779InternalIsOnCheckList (\r
94b17fa1 1780 IN CONST CHAR16 *Name,\r
1781 IN CONST SHELL_PARAM_ITEM *CheckList,\r
252d9457 1782 OUT SHELL_PARAM_TYPE *Type\r
a405b86d 1783 )\r
1784{\r
94b17fa1 1785 SHELL_PARAM_ITEM *TempListItem;\r
252d9457 1786 CHAR16 *TempString;\r
94b17fa1 1787\r
1788 //\r
1789 // ASSERT that all 3 pointer parameters aren't NULL\r
1790 //\r
1791 ASSERT(CheckList != NULL);\r
1792 ASSERT(Type != NULL);\r
1793 ASSERT(Name != NULL);\r
1794\r
d2b4564b 1795 //\r
1796 // question mark and page break mode are always supported\r
1797 //\r
1798 if ((StrCmp(Name, L"-?") == 0) ||\r
1799 (StrCmp(Name, L"-b") == 0)\r
a405b86d 1800 ) {\r
252d9457 1801 *Type = TypeFlag;\r
d2b4564b 1802 return (TRUE);\r
1803 }\r
1804\r
94b17fa1 1805 //\r
1806 // Enumerate through the list\r
1807 //\r
1808 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1809 //\r
9eb53ac3 1810 // If the Type is TypeStart only check the first characters of the passed in param\r
1811 // If it matches set the type and return TRUE\r
94b17fa1 1812 //\r
b0934ac4 1813 if (TempListItem->Type == TypeStart) {\r
252d9457 1814 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1815 *Type = TempListItem->Type;\r
1816 return (TRUE);\r
1817 }\r
1818 TempString = NULL;\r
1819 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));\r
1820 if (TempString != NULL) {\r
1821 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {\r
1822 *Type = TempListItem->Type;\r
1823 FreePool(TempString);\r
1824 return (TRUE);\r
1825 }\r
1826 FreePool(TempString);\r
1827 }\r
1828 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {\r
94b17fa1 1829 *Type = TempListItem->Type;\r
1830 return (TRUE);\r
1831 }\r
1832 }\r
2247dde4 1833\r
94b17fa1 1834 return (FALSE);\r
1835}\r
1836/**\r
d2b4564b 1837 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 1838\r
a405b86d 1839 @param[in] Name pointer to Name of parameter found\r
1840 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.\r
94b17fa1 1841\r
1842 @retval TRUE the Parameter is a flag.\r
a405b86d 1843 @retval FALSE the Parameter not a flag.\r
94b17fa1 1844**/\r
1845BOOLEAN\r
1846EFIAPI\r
d2b4564b 1847InternalIsFlag (\r
2247dde4 1848 IN CONST CHAR16 *Name,\r
1849 IN BOOLEAN AlwaysAllowNumbers\r
94b17fa1 1850 )\r
1851{\r
1852 //\r
1853 // ASSERT that Name isn't NULL\r
1854 //\r
1855 ASSERT(Name != NULL);\r
1856\r
2247dde4 1857 //\r
1858 // If we accept numbers then dont return TRUE. (they will be values)\r
1859 //\r
a405b86d 1860 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {\r
2247dde4 1861 return (FALSE);\r
1862 }\r
1863\r
94b17fa1 1864 //\r
a405b86d 1865 // If the Name has a /, +, or - as the first character return TRUE\r
94b17fa1 1866 //\r
1e6e84c7 1867 if ((Name[0] == L'/') ||\r
d2b4564b 1868 (Name[0] == L'-') ||\r
1869 (Name[0] == L'+')\r
a405b86d 1870 ) {\r
94b17fa1 1871 return (TRUE);\r
1872 }\r
1873 return (FALSE);\r
1874}\r
1875\r
1876/**\r
1e6e84c7 1877 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 1878\r
1879 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 1880\r
a405b86d 1881 @param[in] CheckList pointer to list of parameters to check\r
1882 @param[out] CheckPackage pointer to pointer to list checked values\r
1883 @param[out] ProblemParam optional pointer to pointer to unicode string for\r
d2b4564b 1884 the paramater that caused failure. If used then the\r
1885 caller is responsible for freeing the memory.\r
a405b86d 1886 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1887 @param[in] Argv pointer to array of parameters\r
1888 @param[in] Argc Count of parameters in Argv\r
1889 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.\r
94b17fa1 1890\r
1891 @retval EFI_SUCCESS The operation completed sucessfully.\r
1892 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1893 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1e6e84c7 1894 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1895 duplicated. the duplicated command line argument\r
94b17fa1 1896 was returned in ProblemParam if provided.\r
1e6e84c7 1897 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
94b17fa1 1898 the invalid command line argument was returned in\r
1899 ProblemParam if provided.\r
1900**/\r
1901EFI_STATUS\r
1902EFIAPI\r
1903InternalCommandLineParse (\r
1904 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1905 OUT LIST_ENTRY **CheckPackage,\r
1906 OUT CHAR16 **ProblemParam OPTIONAL,\r
1907 IN BOOLEAN AutoPageBreak,\r
1908 IN CONST CHAR16 **Argv,\r
2247dde4 1909 IN UINTN Argc,\r
1910 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 1911 )\r
1912{\r
94b17fa1 1913 UINTN LoopCounter;\r
252d9457 1914 SHELL_PARAM_TYPE CurrentItemType;\r
94b17fa1 1915 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
125c2cf4 1916 UINTN GetItemValue;\r
1917 UINTN ValueSize;\r
a405b86d 1918 UINTN Count;\r
252d9457 1919 CONST CHAR16 *TempPointer;\r
94b17fa1 1920\r
1921 CurrentItemPackage = NULL;\r
125c2cf4 1922 GetItemValue = 0;\r
1923 ValueSize = 0;\r
a405b86d 1924 Count = 0;\r
94b17fa1 1925\r
1926 //\r
1927 // If there is only 1 item we dont need to do anything\r
1928 //\r
a405b86d 1929 if (Argc < 1) {\r
94b17fa1 1930 *CheckPackage = NULL;\r
1931 return (EFI_SUCCESS);\r
1932 }\r
1933\r
2247dde4 1934 //\r
1935 // ASSERTs\r
1936 //\r
1937 ASSERT(CheckList != NULL);\r
1938 ASSERT(Argv != NULL);\r
1939\r
94b17fa1 1940 //\r
1941 // initialize the linked list\r
1942 //\r
1943 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
02a758cb 1944 if (*CheckPackage == NULL) {\r
beab0fc5 1945 return (EFI_OUT_OF_RESOURCES);\r
02a758cb 1946 }\r
beab0fc5 1947\r
94b17fa1 1948 InitializeListHead(*CheckPackage);\r
1949\r
1950 //\r
1951 // loop through each of the arguments\r
1952 //\r
1953 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1954 if (Argv[LoopCounter] == NULL) {\r
1955 //\r
1956 // do nothing for NULL argv\r
1957 //\r
a405b86d 1958 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
2247dde4 1959 //\r
1960 // We might have leftover if last parameter didnt have optional value\r
1961 //\r
125c2cf4 1962 if (GetItemValue != 0) {\r
1963 GetItemValue = 0;\r
2247dde4 1964 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1965 }\r
94b17fa1 1966 //\r
1967 // this is a flag\r
1968 //\r
252d9457 1969 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 1970 if (CurrentItemPackage == NULL) {\r
1971 ShellCommandLineFreeVarList(*CheckPackage);\r
1972 *CheckPackage = NULL;\r
1973 return (EFI_OUT_OF_RESOURCES);\r
1974 }\r
252d9457 1975 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
beab0fc5 1976 if (CurrentItemPackage->Name == NULL) {\r
1977 ShellCommandLineFreeVarList(*CheckPackage);\r
1978 *CheckPackage = NULL;\r
1979 return (EFI_OUT_OF_RESOURCES);\r
1980 }\r
94b17fa1 1981 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1982 CurrentItemPackage->Type = CurrentItemType;\r
1983 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 1984 CurrentItemPackage->Value = NULL;\r
94b17fa1 1985\r
1986 //\r
1987 // Does this flag require a value\r
1988 //\r
125c2cf4 1989 switch (CurrentItemPackage->Type) {\r
94b17fa1 1990 //\r
125c2cf4 1991 // possibly trigger the next loop(s) to populate the value of this item\r
1e6e84c7 1992 //\r
125c2cf4 1993 case TypeValue:\r
1e6e84c7 1994 GetItemValue = 1;\r
125c2cf4 1995 ValueSize = 0;\r
1996 break;\r
1997 case TypeDoubleValue:\r
1998 GetItemValue = 2;\r
1999 ValueSize = 0;\r
2000 break;\r
2001 case TypeMaxValue:\r
2002 GetItemValue = (UINTN)(-1);\r
2003 ValueSize = 0;\r
2004 break;\r
2005 default:\r
2006 //\r
2007 // this item has no value expected; we are done\r
2008 //\r
2009 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2010 ASSERT(GetItemValue == 0);\r
2011 break;\r
94b17fa1 2012 }\r
a405b86d 2013 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {\r
b1f95a06 2014 ASSERT(CurrentItemPackage != NULL);\r
2015 //\r
125c2cf4 2016 // get the item VALUE for a previous flag\r
b1f95a06 2017 //\r
49bd498d 2018 if (StrStr(Argv[LoopCounter], L" ") == NULL) {\r
2019 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
2020 ASSERT(CurrentItemPackage->Value != NULL);\r
2021 if (ValueSize == 0) {\r
2022 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2023 } else {\r
2024 StrCat(CurrentItemPackage->Value, L" ");\r
2025 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2026 }\r
2027 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
125c2cf4 2028 } else {\r
49bd498d 2029 //\r
2030 // the parameter has spaces. must be quoted.\r
2031 //\r
2032 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value);\r
2033 ASSERT(CurrentItemPackage->Value != NULL);\r
2034 if (ValueSize == 0) {\r
2035 StrCpy(CurrentItemPackage->Value, L"\"");\r
2036 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2037 StrCat(CurrentItemPackage->Value, L"\"");\r
2038 } else {\r
2039 StrCat(CurrentItemPackage->Value, L" ");\r
2040 StrCat(CurrentItemPackage->Value, L"\"");\r
2041 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2042 StrCat(CurrentItemPackage->Value, L"\"");\r
2043 }\r
2044 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
125c2cf4 2045 }\r
125c2cf4 2046 GetItemValue--;\r
2047 if (GetItemValue == 0) {\r
2048 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2049 }\r
a405b86d 2050 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {\r
b1f95a06 2051 //\r
2052 // add this one as a non-flag\r
2053 //\r
252d9457 2054\r
2055 TempPointer = Argv[LoopCounter];\r
b0934ac4 2056 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')\r
252d9457 2057 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
2058 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
2059 ){\r
2060 TempPointer++;\r
2061 }\r
2062 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
beab0fc5 2063 if (CurrentItemPackage == NULL) {\r
2064 ShellCommandLineFreeVarList(*CheckPackage);\r
2065 *CheckPackage = NULL;\r
2066 return (EFI_OUT_OF_RESOURCES);\r
2067 }\r
b1f95a06 2068 CurrentItemPackage->Name = NULL;\r
2069 CurrentItemPackage->Type = TypePosition;\r
252d9457 2070 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));\r
beab0fc5 2071 if (CurrentItemPackage->Value == NULL) {\r
2072 ShellCommandLineFreeVarList(*CheckPackage);\r
2073 *CheckPackage = NULL;\r
2074 return (EFI_OUT_OF_RESOURCES);\r
2075 }\r
252d9457 2076 StrCpy(CurrentItemPackage->Value, TempPointer);\r
a405b86d 2077 CurrentItemPackage->OriginalPosition = Count++;\r
9b3bf083 2078 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
252d9457 2079 } else {\r
94b17fa1 2080 //\r
2081 // this was a non-recognised flag... error!\r
2082 //\r
252d9457 2083 if (ProblemParam != NULL) {\r
2084 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
beab0fc5 2085 if (*ProblemParam != NULL) {\r
b0934ac4 2086 StrCpy(*ProblemParam, Argv[LoopCounter]);\r
beab0fc5 2087 }\r
252d9457 2088 }\r
94b17fa1 2089 ShellCommandLineFreeVarList(*CheckPackage);\r
2090 *CheckPackage = NULL;\r
2091 return (EFI_VOLUME_CORRUPTED);\r
94b17fa1 2092 }\r
2093 }\r
125c2cf4 2094 if (GetItemValue != 0) {\r
2095 GetItemValue = 0;\r
2096 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2097 }\r
94b17fa1 2098 //\r
2099 // support for AutoPageBreak\r
2100 //\r
2101 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
2102 ShellSetPageBreakMode(TRUE);\r
2103 }\r
2104 return (EFI_SUCCESS);\r
2105}\r
2106\r
2107/**\r
1e6e84c7 2108 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 2109 Optionally removes NULL values first.\r
1e6e84c7 2110\r
94b17fa1 2111 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 2112\r
a405b86d 2113 @param[in] CheckList The pointer to list of parameters to check.\r
2114 @param[out] CheckPackage The package of checked values.\r
2115 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
94b17fa1 2116 the paramater that caused failure.\r
a405b86d 2117 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2118 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
94b17fa1 2119\r
2120 @retval EFI_SUCCESS The operation completed sucessfully.\r
a405b86d 2121 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2122 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2123 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2124 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
1e6e84c7 2125 of the command line arguments was returned in\r
94b17fa1 2126 ProblemParam if provided.\r
a405b86d 2127 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2128 The invalid command line argument was returned in\r
94b17fa1 2129 ProblemParam if provided.\r
2130**/\r
2131EFI_STATUS\r
2132EFIAPI\r
2247dde4 2133ShellCommandLineParseEx (\r
94b17fa1 2134 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2135 OUT LIST_ENTRY **CheckPackage,\r
2136 OUT CHAR16 **ProblemParam OPTIONAL,\r
2247dde4 2137 IN BOOLEAN AutoPageBreak,\r
2138 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 2139 )\r
2140{\r
1e6e84c7 2141 //\r
94b17fa1 2142 // ASSERT that CheckList and CheckPackage aren't NULL\r
2143 //\r
2144 ASSERT(CheckList != NULL);\r
2145 ASSERT(CheckPackage != NULL);\r
2146\r
1e6e84c7 2147 //\r
94b17fa1 2148 // Check for UEFI Shell 2.0 protocols\r
2149 //\r
366f81a0 2150 if (gEfiShellParametersProtocol != NULL) {\r
1e6e84c7 2151 return (InternalCommandLineParse(CheckList,\r
2152 CheckPackage,\r
2153 ProblemParam,\r
2154 AutoPageBreak,\r
366f81a0 2155 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,\r
2156 gEfiShellParametersProtocol->Argc,\r
2247dde4 2157 AlwaysAllowNumbers));\r
94b17fa1 2158 }\r
2159\r
1e6e84c7 2160 //\r
94b17fa1 2161 // ASSERT That EFI Shell is not required\r
2162 //\r
2163 ASSERT (mEfiShellInterface != NULL);\r
1e6e84c7 2164 return (InternalCommandLineParse(CheckList,\r
2165 CheckPackage,\r
2166 ProblemParam,\r
2167 AutoPageBreak,\r
08d7f8e8 2168 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2247dde4 2169 mEfiShellInterface->Argc,\r
2170 AlwaysAllowNumbers));\r
94b17fa1 2171}\r
2172\r
2173/**\r
2174 Frees shell variable list that was returned from ShellCommandLineParse.\r
2175\r
2176 This function will free all the memory that was used for the CheckPackage\r
2177 list of postprocessed shell arguments.\r
2178\r
2179 this function has no return value.\r
2180\r
2181 if CheckPackage is NULL, then return\r
2182\r
2183 @param CheckPackage the list to de-allocate\r
2184 **/\r
2185VOID\r
2186EFIAPI\r
2187ShellCommandLineFreeVarList (\r
2188 IN LIST_ENTRY *CheckPackage\r
a405b86d 2189 )\r
2190{\r
94b17fa1 2191 LIST_ENTRY *Node;\r
2192\r
2193 //\r
2194 // check for CheckPackage == NULL\r
2195 //\r
2196 if (CheckPackage == NULL) {\r
2197 return;\r
2198 }\r
2199\r
2200 //\r
2201 // for each node in the list\r
2202 //\r
9eb53ac3 2203 for ( Node = GetFirstNode(CheckPackage)\r
a405b86d 2204 ; !IsListEmpty(CheckPackage)\r
9eb53ac3 2205 ; Node = GetFirstNode(CheckPackage)\r
a405b86d 2206 ){\r
94b17fa1 2207 //\r
2208 // Remove it from the list\r
2209 //\r
2210 RemoveEntryList(Node);\r
2211\r
2212 //\r
2213 // if it has a name free the name\r
2214 //\r
2215 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2216 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2217 }\r
2218\r
2219 //\r
2220 // if it has a value free the value\r
2221 //\r
2222 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2223 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2224 }\r
1e6e84c7 2225\r
94b17fa1 2226 //\r
2227 // free the node structure\r
2228 //\r
2229 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2230 }\r
2231 //\r
2232 // free the list head node\r
2233 //\r
2234 FreePool(CheckPackage);\r
2235}\r
2236/**\r
2237 Checks for presence of a flag parameter\r
2238\r
2239 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2240\r
2241 if CheckPackage is NULL then return FALSE.\r
2242 if KeyString is NULL then ASSERT()\r
1e6e84c7 2243\r
94b17fa1 2244 @param CheckPackage The package of parsed command line arguments\r
2245 @param KeyString the Key of the command line argument to check for\r
2246\r
2247 @retval TRUE the flag is on the command line\r
2248 @retval FALSE the flag is not on the command line\r
2249 **/\r
2250BOOLEAN\r
2251EFIAPI\r
2252ShellCommandLineGetFlag (\r
a405b86d 2253 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2254 IN CONST CHAR16 * CONST KeyString\r
2255 )\r
2256{\r
94b17fa1 2257 LIST_ENTRY *Node;\r
252d9457 2258 CHAR16 *TempString;\r
94b17fa1 2259\r
2260 //\r
0c1950ba 2261 // return FALSE for no package or KeyString is NULL\r
94b17fa1 2262 //\r
0c1950ba 2263 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2264 return (FALSE);\r
2265 }\r
2266\r
2267 //\r
2268 // enumerate through the list of parametrs\r
2269 //\r
1e6e84c7 2270 for ( Node = GetFirstNode(CheckPackage)\r
2271 ; !IsNull (CheckPackage, Node)\r
2272 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2273 ){\r
94b17fa1 2274 //\r
2275 // If the Name matches, return TRUE (and there may be NULL name)\r
2276 //\r
2277 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2278 //\r
2279 // If Type is TypeStart then only compare the begining of the strings\r
2280 //\r
252d9457 2281 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2282 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2283 return (TRUE);\r
2284 }\r
2285 TempString = NULL;\r
2286 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2287 if (TempString != NULL) {\r
2288 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2289 FreePool(TempString);\r
2290 return (TRUE);\r
2291 }\r
2292 FreePool(TempString);\r
2293 }\r
2294 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2295 return (TRUE);\r
2296 }\r
2297 }\r
2298 }\r
2299 return (FALSE);\r
2300}\r
2301/**\r
a405b86d 2302 Returns value from command line argument.\r
94b17fa1 2303\r
a405b86d 2304 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
1e6e84c7 2305\r
a405b86d 2306 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2307\r
a405b86d 2308 @param[in] CheckPackage The package of parsed command line arguments.\r
2309 @param[in] KeyString The Key of the command line argument to check for.\r
94b17fa1 2310\r
a405b86d 2311 @retval NULL The flag is not on the command line.\r
2312 @retval !=NULL The pointer to unicode string of the value.\r
2313**/\r
94b17fa1 2314CONST CHAR16*\r
2315EFIAPI\r
2316ShellCommandLineGetValue (\r
2317 IN CONST LIST_ENTRY *CheckPackage,\r
2318 IN CHAR16 *KeyString\r
a405b86d 2319 )\r
2320{\r
94b17fa1 2321 LIST_ENTRY *Node;\r
252d9457 2322 CHAR16 *TempString;\r
94b17fa1 2323\r
2324 //\r
0c1950ba 2325 // return NULL for no package or KeyString is NULL\r
94b17fa1 2326 //\r
0c1950ba 2327 if (CheckPackage == NULL || KeyString == NULL) {\r
94b17fa1 2328 return (NULL);\r
2329 }\r
2330\r
2331 //\r
2332 // enumerate through the list of parametrs\r
2333 //\r
1e6e84c7 2334 for ( Node = GetFirstNode(CheckPackage)\r
2335 ; !IsNull (CheckPackage, Node)\r
2336 ; Node = GetNextNode(CheckPackage, Node)\r
252d9457 2337 ){\r
94b17fa1 2338 //\r
252d9457 2339 // If the Name matches, return TRUE (and there may be NULL name)\r
94b17fa1 2340 //\r
2341 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2342 //\r
2343 // If Type is TypeStart then only compare the begining of the strings\r
2344 //\r
252d9457 2345 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2346 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2347 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2348 }\r
2349 TempString = NULL;\r
2350 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2351 if (TempString != NULL) {\r
2352 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2353 FreePool(TempString);\r
2354 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2355 }\r
2356 FreePool(TempString);\r
2357 }\r
2358 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2359 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2360 }\r
2361 }\r
2362 }\r
2363 return (NULL);\r
2364}\r
a405b86d 2365\r
94b17fa1 2366/**\r
a405b86d 2367 Returns raw value from command line argument.\r
94b17fa1 2368\r
a405b86d 2369 Raw value parameters are in the form of "value" in a specific position in the list.\r
1e6e84c7 2370\r
a405b86d 2371 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2372\r
a405b86d 2373 @param[in] CheckPackage The package of parsed command line arguments.\r
2374 @param[in] Position The position of the value.\r
94b17fa1 2375\r
a405b86d 2376 @retval NULL The flag is not on the command line.\r
2377 @retval !=NULL The pointer to unicode string of the value.\r
94b17fa1 2378 **/\r
2379CONST CHAR16*\r
2380EFIAPI\r
2381ShellCommandLineGetRawValue (\r
a405b86d 2382 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2383 IN UINTN Position\r
2384 )\r
2385{\r
94b17fa1 2386 LIST_ENTRY *Node;\r
2387\r
2388 //\r
2389 // check for CheckPackage == NULL\r
2390 //\r
2391 if (CheckPackage == NULL) {\r
2392 return (NULL);\r
2393 }\r
2394\r
2395 //\r
2396 // enumerate through the list of parametrs\r
2397 //\r
1e6e84c7 2398 for ( Node = GetFirstNode(CheckPackage)\r
2399 ; !IsNull (CheckPackage, Node)\r
2400 ; Node = GetNextNode(CheckPackage, Node)\r
a405b86d 2401 ){\r
94b17fa1 2402 //\r
2403 // If the position matches, return the value\r
2404 //\r
2405 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2406 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2407 }\r
2408 }\r
2409 return (NULL);\r
b1f95a06 2410}\r
2247dde4 2411\r
2412/**\r
1e6e84c7 2413 returns the number of command line value parameters that were parsed.\r
2414\r
2247dde4 2415 this will not include flags.\r
2416\r
a405b86d 2417 @param[in] CheckPackage The package of parsed command line arguments.\r
2418\r
2247dde4 2419 @retval (UINTN)-1 No parsing has ocurred\r
2420 @return other The number of value parameters found\r
2421**/\r
2422UINTN\r
2423EFIAPI\r
2424ShellCommandLineGetCount(\r
a405b86d 2425 IN CONST LIST_ENTRY *CheckPackage\r
125c2cf4 2426 )\r
2427{\r
a405b86d 2428 LIST_ENTRY *Node1;\r
2429 UINTN Count;\r
2430\r
2431 if (CheckPackage == NULL) {\r
2432 return (0);\r
2433 }\r
2434 for ( Node1 = GetFirstNode(CheckPackage), Count = 0\r
2435 ; !IsNull (CheckPackage, Node1)\r
2436 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2437 ){\r
2438 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {\r
2439 Count++;\r
2440 }\r
2441 }\r
2442 return (Count);\r
2247dde4 2443}\r
2444\r
36a9d672 2445/**\r
2446 Determins if a parameter is duplicated.\r
2447\r
1e6e84c7 2448 If Param is not NULL then it will point to a callee allocated string buffer\r
36a9d672 2449 with the parameter value if a duplicate is found.\r
2450\r
2451 If CheckPackage is NULL, then ASSERT.\r
2452\r
2453 @param[in] CheckPackage The package of parsed command line arguments.\r
2454 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2455\r
2456 @retval EFI_SUCCESS No parameters were duplicated.\r
2457 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2458 **/\r
2459EFI_STATUS\r
2460EFIAPI\r
2461ShellCommandLineCheckDuplicate (\r
2462 IN CONST LIST_ENTRY *CheckPackage,\r
2463 OUT CHAR16 **Param\r
2464 )\r
2465{\r
2466 LIST_ENTRY *Node1;\r
2467 LIST_ENTRY *Node2;\r
1e6e84c7 2468\r
36a9d672 2469 ASSERT(CheckPackage != NULL);\r
2470\r
1e6e84c7 2471 for ( Node1 = GetFirstNode(CheckPackage)\r
2472 ; !IsNull (CheckPackage, Node1)\r
2473 ; Node1 = GetNextNode(CheckPackage, Node1)\r
a405b86d 2474 ){\r
1e6e84c7 2475 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2476 ; !IsNull (CheckPackage, Node2)\r
2477 ; Node2 = GetNextNode(CheckPackage, Node2)\r
a405b86d 2478 ){\r
2479 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 2480 if (Param != NULL) {\r
2481 *Param = NULL;\r
2482 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2483 }\r
2484 return (EFI_DEVICE_ERROR);\r
2485 }\r
2486 }\r
2487 }\r
2488 return (EFI_SUCCESS);\r
2489}\r
2490\r
975136ab 2491/**\r
1e6e84c7 2492 This is a find and replace function. Upon successful return the NewString is a copy of\r
975136ab 2493 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2494\r
b3011f40 2495 If SourceString and NewString overlap the behavior is undefined.\r
2496\r
975136ab 2497 If the string would grow bigger than NewSize it will halt and return error.\r
2498\r
4ff7e37b
ED
2499 @param[in] SourceString The string with source buffer.\r
2500 @param[in, out] NewString The string with resultant buffer.\r
2501 @param[in] NewSize The size in bytes of NewString.\r
2502 @param[in] FindTarget The string to look for.\r
2503 @param[in] ReplaceWith The string to replace FindTarget with.\r
2504 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2505 immediately before it.\r
2506 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
969c783b 2507\r
2508 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2509 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2510 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2511 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2512 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2513 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
1e6e84c7 2514 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
969c783b 2515 the new string (truncation occurred).\r
a405b86d 2516 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
975136ab 2517**/\r
975136ab 2518EFI_STATUS\r
2519EFIAPI\r
a405b86d 2520ShellCopySearchAndReplace(\r
975136ab 2521 IN CHAR16 CONST *SourceString,\r
a405b86d 2522 IN OUT CHAR16 *NewString,\r
975136ab 2523 IN UINTN NewSize,\r
2524 IN CONST CHAR16 *FindTarget,\r
969c783b 2525 IN CONST CHAR16 *ReplaceWith,\r
a405b86d 2526 IN CONST BOOLEAN SkipPreCarrot,\r
2527 IN CONST BOOLEAN ParameterReplacing\r
1e6e84c7 2528 )\r
2247dde4 2529{\r
0158294b 2530 UINTN Size;\r
a405b86d 2531 CHAR16 *Replace;\r
2532\r
975136ab 2533 if ( (SourceString == NULL)\r
2534 || (NewString == NULL)\r
2535 || (FindTarget == NULL)\r
2536 || (ReplaceWith == NULL)\r
2537 || (StrLen(FindTarget) < 1)\r
2538 || (StrLen(SourceString) < 1)\r
a405b86d 2539 ){\r
975136ab 2540 return (EFI_INVALID_PARAMETER);\r
2541 }\r
a405b86d 2542 Replace = NULL;\r
2543 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {\r
2544 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
2545 } else {\r
2546 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
beab0fc5 2547 if (Replace != NULL) {\r
2548 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
2549 }\r
a405b86d 2550 }\r
3e082d58 2551 if (Replace == NULL) {\r
2552 return (EFI_OUT_OF_RESOURCES);\r
2553 }\r
2247dde4 2554 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2555 while (*SourceString != CHAR_NULL) {\r
969c783b 2556 //\r
a405b86d 2557 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
969c783b 2558 // dont have a carrot do a replace...\r
2559 //\r
1e6e84c7 2560 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
a405b86d 2561 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)\r
2562 ){\r
975136ab 2563 SourceString += StrLen(FindTarget);\r
0158294b 2564 Size = StrSize(NewString);\r
a405b86d 2565 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {\r
2566 FreePool(Replace);\r
975136ab 2567 return (EFI_BUFFER_TOO_SMALL);\r
2568 }\r
a405b86d 2569 StrCat(NewString, Replace);\r
975136ab 2570 } else {\r
0158294b 2571 Size = StrSize(NewString);\r
2572 if (Size + sizeof(CHAR16) > NewSize) {\r
a405b86d 2573 FreePool(Replace);\r
975136ab 2574 return (EFI_BUFFER_TOO_SMALL);\r
2575 }\r
2576 StrnCat(NewString, SourceString, 1);\r
2577 SourceString++;\r
2578 }\r
2579 }\r
a405b86d 2580 FreePool(Replace);\r
975136ab 2581 return (EFI_SUCCESS);\r
2582}\r
b1f95a06 2583\r
e2f8297f 2584/**\r
2585 Internal worker function to output a string.\r
2586\r
2587 This function will output a string to the correct StdOut.\r
2588\r
2589 @param[in] String The string to print out.\r
2590\r
2591 @retval EFI_SUCCESS The operation was sucessful.\r
2592 @retval !EFI_SUCCESS The operation failed.\r
2593**/\r
2594EFI_STATUS\r
2595EFIAPI\r
2596InternalPrintTo (\r
2597 IN CONST CHAR16 *String\r
2598 )\r
2599{\r
2600 UINTN Size;\r
2601 Size = StrSize(String) - sizeof(CHAR16);\r
a405b86d 2602 if (Size == 0) {\r
2603 return (EFI_SUCCESS);\r
2604 }\r
366f81a0 2605 if (gEfiShellParametersProtocol != NULL) {\r
2606 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
e2f8297f 2607 }\r
2608 if (mEfiShellInterface != NULL) {\r
06c355b4 2609 if (mEfiShellInterface->RedirArgc == 0) { \r
49bd498d 2610 //\r
2611 // Divide in half for old shell. Must be string length not size.\r
06c355b4 2612 // \r
2613 Size /=2; // Divide in half only when no redirection.\r
2614 }\r
a405b86d 2615 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
e2f8297f 2616 }\r
2617 ASSERT(FALSE);\r
2618 return (EFI_UNSUPPORTED);\r
2619}\r
2620\r
b1f95a06 2621/**\r
2622 Print at a specific location on the screen.\r
2623\r
f1b87e7a 2624 This function will move the cursor to a given screen location and print the specified string\r
1e6e84c7 2625\r
2626 If -1 is specified for either the Row or Col the current screen location for BOTH\r
f1b87e7a 2627 will be used.\r
b1f95a06 2628\r
2629 if either Row or Col is out of range for the current console, then ASSERT\r
2630 if Format is NULL, then ASSERT\r
2631\r
1e6e84c7 2632 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
b1f95a06 2633 the following additional flags:\r
2634 %N - Set output attribute to normal\r
2635 %H - Set output attribute to highlight\r
2636 %E - Set output attribute to error\r
2637 %B - Set output attribute to blue color\r
2638 %V - Set output attribute to green color\r
2639\r
2640 Note: The background color is controlled by the shell command cls.\r
2641\r
b1f95a06 2642 @param[in] Col the column to print at\r
252d9457 2643 @param[in] Row the row to print at\r
b1f95a06 2644 @param[in] Format the format string\r
2247dde4 2645 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2646\r
a405b86d 2647 @return EFI_SUCCESS The operation was successful.\r
2648 @return EFI_DEVICE_ERROR The console device reported an error.\r
b1f95a06 2649**/\r
a405b86d 2650EFI_STATUS\r
b1f95a06 2651EFIAPI\r
2247dde4 2652InternalShellPrintWorker(\r
b1f95a06 2653 IN INT32 Col OPTIONAL,\r
2654 IN INT32 Row OPTIONAL,\r
2655 IN CONST CHAR16 *Format,\r
252d9457 2656 IN VA_LIST Marker\r
1e6e84c7 2657 )\r
2247dde4 2658{\r
b1f95a06 2659 EFI_STATUS Status;\r
975136ab 2660 CHAR16 *ResumeLocation;\r
2661 CHAR16 *FormatWalker;\r
a405b86d 2662 UINTN OriginalAttribute;\r
89e8537a 2663 CHAR16 *mPostReplaceFormat;\r
2664 CHAR16 *mPostReplaceFormat2;\r
2665\r
2666 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2667 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
a405b86d 2668\r
f8d3e689 2669 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
2670 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2671 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2672 return (EFI_OUT_OF_RESOURCES);\r
2673 }\r
2674\r
a405b86d 2675 Status = EFI_SUCCESS;\r
2676 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
1e6e84c7 2677\r
975136ab 2678 //\r
2679 // Back and forth each time fixing up 1 of our flags...\r
2680 //\r
a405b86d 2681 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
975136ab 2682 ASSERT_EFI_ERROR(Status);\r
a405b86d 2683 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
975136ab 2684 ASSERT_EFI_ERROR(Status);\r
a405b86d 2685 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
975136ab 2686 ASSERT_EFI_ERROR(Status);\r
a405b86d 2687 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
975136ab 2688 ASSERT_EFI_ERROR(Status);\r
a405b86d 2689 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
975136ab 2690 ASSERT_EFI_ERROR(Status);\r
2691\r
2692 //\r
2693 // Use the last buffer from replacing to print from...\r
2694 //\r
a405b86d 2695 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
b1f95a06 2696\r
2697 if (Col != -1 && Row != -1) {\r
b1f95a06 2698 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
975136ab 2699 }\r
2700\r
ecd3d59f 2701 FormatWalker = mPostReplaceFormat2;\r
2247dde4 2702 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2703 //\r
2704 // Find the next attribute change request\r
2705 //\r
2706 ResumeLocation = StrStr(FormatWalker, L"%");\r
2707 if (ResumeLocation != NULL) {\r
2247dde4 2708 *ResumeLocation = CHAR_NULL;\r
975136ab 2709 }\r
2710 //\r
2711 // print the current FormatWalker string\r
2712 //\r
a405b86d 2713 if (StrLen(FormatWalker)>0) {\r
2714 Status = InternalPrintTo(FormatWalker);\r
2715 if (EFI_ERROR(Status)) {\r
2716 break;\r
2717 }\r
2718 }\r
2719\r
975136ab 2720 //\r
2721 // update the attribute\r
2722 //\r
2723 if (ResumeLocation != NULL) {\r
5d46f17b 2724 if (*(ResumeLocation-1) == L'^') {\r
8bb7441e 2725 //\r
2726 // Move cursor back 1 position to overwrite the ^\r
2727 //\r
2728 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);\r
2729\r
5d46f17b 2730 //\r
2731 // Print a simple '%' symbol\r
2732 //\r
2733 Status = InternalPrintTo(L"%");\r
2734 ResumeLocation = ResumeLocation - 1;\r
2735 } else {\r
2736 switch (*(ResumeLocation+1)) {\r
2737 case (L'N'):\r
2738 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
a405b86d 2739 break;\r
5d46f17b 2740 case (L'E'):\r
2741 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2742 break;\r
2743 case (L'H'):\r
2744 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2745 break;\r
2746 case (L'B'):\r
2747 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2748 break;\r
2749 case (L'V'):\r
2750 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2751 break;\r
2752 default:\r
2753 //\r
2754 // Print a simple '%' symbol\r
2755 //\r
2756 Status = InternalPrintTo(L"%");\r
2757 if (EFI_ERROR(Status)) {\r
2758 break;\r
2759 }\r
2760 ResumeLocation = ResumeLocation - 1;\r
2761 break;\r
2762 }\r
975136ab 2763 }\r
2764 } else {\r
2765 //\r
2766 // reset to normal now...\r
2767 //\r
975136ab 2768 break;\r
2769 }\r
2770\r
2771 //\r
2772 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2773 //\r
2774 FormatWalker = ResumeLocation + 2;\r
2775 }\r
b1f95a06 2776\r
a405b86d 2777 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
89e8537a 2778\r
2779 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2780 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
a405b86d 2781 return (Status);\r
5f7431d0 2782}\r
2247dde4 2783\r
2784/**\r
2785 Print at a specific location on the screen.\r
2786\r
e2f8297f 2787 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2788\r
2789 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2247dde4 2790 will be used.\r
2791\r
e2f8297f 2792 If either Row or Col is out of range for the current console, then ASSERT.\r
2793 If Format is NULL, then ASSERT.\r
2247dde4 2794\r
1e6e84c7 2795 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2796 the following additional flags:\r
2797 %N - Set output attribute to normal\r
2798 %H - Set output attribute to highlight\r
2799 %E - Set output attribute to error\r
2800 %B - Set output attribute to blue color\r
2801 %V - Set output attribute to green color\r
2802\r
2803 Note: The background color is controlled by the shell command cls.\r
2804\r
2247dde4 2805 @param[in] Col the column to print at\r
a405b86d 2806 @param[in] Row the row to print at\r
2247dde4 2807 @param[in] Format the format string\r
a405b86d 2808 @param[in] ... The variable argument list.\r
2247dde4 2809\r
a405b86d 2810 @return EFI_SUCCESS The printing was successful.\r
2811 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2812**/\r
a405b86d 2813EFI_STATUS\r
2247dde4 2814EFIAPI\r
2815ShellPrintEx(\r
2816 IN INT32 Col OPTIONAL,\r
2817 IN INT32 Row OPTIONAL,\r
2818 IN CONST CHAR16 *Format,\r
2819 ...\r
1e6e84c7 2820 )\r
2247dde4 2821{\r
2822 VA_LIST Marker;\r
a405b86d 2823 EFI_STATUS RetVal;\r
3e082d58 2824 if (Format == NULL) {\r
2825 return (EFI_INVALID_PARAMETER);\r
2826 }\r
2247dde4 2827 VA_START (Marker, Format);\r
a405b86d 2828 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
e2f8297f 2829 VA_END(Marker);\r
a405b86d 2830 return(RetVal);\r
2247dde4 2831}\r
2832\r
2833/**\r
2834 Print at a specific location on the screen.\r
2835\r
e2f8297f 2836 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 2837\r
2838 If -1 is specified for either the Row or Col the current screen location for BOTH\r
e2f8297f 2839 will be used.\r
2247dde4 2840\r
e2f8297f 2841 If either Row or Col is out of range for the current console, then ASSERT.\r
2842 If Format is NULL, then ASSERT.\r
2247dde4 2843\r
1e6e84c7 2844 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 2845 the following additional flags:\r
1e6e84c7 2846 %N - Set output attribute to normal.\r
2847 %H - Set output attribute to highlight.\r
2848 %E - Set output attribute to error.\r
2849 %B - Set output attribute to blue color.\r
2850 %V - Set output attribute to green color.\r
2247dde4 2851\r
2852 Note: The background color is controlled by the shell command cls.\r
2853\r
1e6e84c7 2854 @param[in] Col The column to print at.\r
a405b86d 2855 @param[in] Row The row to print at.\r
1e6e84c7 2856 @param[in] Language The language of the string to retrieve. If this parameter\r
2857 is NULL, then the current platform language is used.\r
2858 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2859 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
a405b86d 2860 @param[in] ... The variable argument list.\r
2247dde4 2861\r
a405b86d 2862 @return EFI_SUCCESS The printing was successful.\r
2863 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 2864**/\r
a405b86d 2865EFI_STATUS\r
2247dde4 2866EFIAPI\r
2867ShellPrintHiiEx(\r
2868 IN INT32 Col OPTIONAL,\r
2869 IN INT32 Row OPTIONAL,\r
1e6e84c7 2870 IN CONST CHAR8 *Language OPTIONAL,\r
2247dde4 2871 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2872 IN CONST EFI_HANDLE HiiFormatHandle,\r
2873 ...\r
2874 )\r
2875{\r
2876 VA_LIST Marker;\r
2877 CHAR16 *HiiFormatString;\r
a405b86d 2878 EFI_STATUS RetVal;\r
2247dde4 2879\r
2880 VA_START (Marker, HiiFormatHandle);\r
1e6e84c7 2881 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2247dde4 2882 ASSERT(HiiFormatString != NULL);\r
2883\r
2884 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2885\r
a405b86d 2886 SHELL_FREE_NON_NULL(HiiFormatString);\r
e2f8297f 2887 VA_END(Marker);\r
2247dde4 2888\r
2889 return (RetVal);\r
2890}\r
2891\r
2892/**\r
2893 Function to determine if a given filename represents a file or a directory.\r
2894\r
2895 @param[in] DirName Path to directory to test.\r
2896\r
c8c22591 2897 @retval EFI_SUCCESS The Path represents a directory\r
2898 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2899 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2900 @return The path failed to open\r
2247dde4 2901**/\r
2902EFI_STATUS\r
2903EFIAPI\r
2904ShellIsDirectory(\r
2905 IN CONST CHAR16 *DirName\r
2906 )\r
2907{\r
2908 EFI_STATUS Status;\r
a405b86d 2909 SHELL_FILE_HANDLE Handle;\r
3e082d58 2910 CHAR16 *TempLocation;\r
2911 CHAR16 *TempLocation2;\r
2247dde4 2912\r
ecd3d59f 2913 ASSERT(DirName != NULL);\r
2914\r
a405b86d 2915 Handle = NULL;\r
2916 TempLocation = NULL;\r
2247dde4 2917\r
2918 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2919 if (EFI_ERROR(Status)) {\r
a405b86d 2920 //\r
2921 // try good logic first.\r
2922 //\r
366f81a0 2923 if (gEfiShellProtocol != NULL) {\r
3e082d58 2924 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
c8c22591 2925 if (TempLocation == NULL) {\r
2926 ShellCloseFile(&Handle);\r
2927 return (EFI_OUT_OF_RESOURCES);\r
2928 }\r
3e082d58 2929 TempLocation2 = StrStr(TempLocation, L":");\r
2930 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
2931 *(TempLocation2+1) = CHAR_NULL;\r
a405b86d 2932 }\r
366f81a0 2933 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
a405b86d 2934 FreePool(TempLocation);\r
2935 return (EFI_SUCCESS);\r
2936 }\r
2937 FreePool(TempLocation);\r
2938 } else {\r
2939 //\r
2940 // probably a map name?!?!!?\r
2941 //\r
2942 TempLocation = StrStr(DirName, L"\\");\r
2943 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {\r
2944 return (EFI_SUCCESS);\r
2945 }\r
2946 }\r
2247dde4 2947 return (Status);\r
2948 }\r
2949\r
2950 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2951 ShellCloseFile(&Handle);\r
2952 return (EFI_SUCCESS);\r
2953 }\r
2954 ShellCloseFile(&Handle);\r
2955 return (EFI_NOT_FOUND);\r
2956}\r
2957\r
36a9d672 2958/**\r
2959 Function to determine if a given filename represents a file.\r
2960\r
2961 @param[in] Name Path to file to test.\r
2962\r
2963 @retval EFI_SUCCESS The Path represents a file.\r
2964 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2965 @retval other The path failed to open.\r
2966**/\r
2967EFI_STATUS\r
2968EFIAPI\r
2969ShellIsFile(\r
2970 IN CONST CHAR16 *Name\r
2971 )\r
2972{\r
2973 EFI_STATUS Status;\r
a405b86d 2974 SHELL_FILE_HANDLE Handle;\r
36a9d672 2975\r
ecd3d59f 2976 ASSERT(Name != NULL);\r
2977\r
36a9d672 2978 Handle = NULL;\r
2979\r
2980 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2981 if (EFI_ERROR(Status)) {\r
2982 return (Status);\r
2983 }\r
2984\r
2985 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2986 ShellCloseFile(&Handle);\r
2987 return (EFI_SUCCESS);\r
2988 }\r
2989 ShellCloseFile(&Handle);\r
2990 return (EFI_NOT_FOUND);\r
2991}\r
2992\r
b3011f40 2993/**\r
2994 Function to determine if a given filename represents a file.\r
2995\r
2996 This will search the CWD and then the Path.\r
2997\r
2998 If Name is NULL, then ASSERT.\r
2999\r
3000 @param[in] Name Path to file to test.\r
3001\r
3002 @retval EFI_SUCCESS The Path represents a file.\r
3003 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3004 @retval other The path failed to open.\r
3005**/\r
3006EFI_STATUS\r
3007EFIAPI\r
3008ShellIsFileInPath(\r
3009 IN CONST CHAR16 *Name\r
a405b86d 3010 )\r
3011{\r
b3011f40 3012 CHAR16 *NewName;\r
3013 EFI_STATUS Status;\r
3014\r
3015 if (!EFI_ERROR(ShellIsFile(Name))) {\r
a405b86d 3016 return (EFI_SUCCESS);\r
b3011f40 3017 }\r
3018\r
3019 NewName = ShellFindFilePath(Name);\r
3020 if (NewName == NULL) {\r
3021 return (EFI_NOT_FOUND);\r
3022 }\r
3023 Status = ShellIsFile(NewName);\r
3024 FreePool(NewName);\r
3025 return (Status);\r
3026}\r
252d9457 3027\r
125c2cf4 3028/**\r
1e6e84c7 3029 Function to determine whether a string is decimal or hex representation of a number\r
125c2cf4 3030 and return the number converted from the string.\r
3031\r
3032 @param[in] String String representation of a number\r
3033\r
252d9457 3034 @return the number\r
3035 @retval (UINTN)(-1) An error ocurred.\r
125c2cf4 3036**/\r
3037UINTN\r
3038EFIAPI\r
3039ShellStrToUintn(\r
3040 IN CONST CHAR16 *String\r
3041 )\r
3042{\r
252d9457 3043 UINT64 RetVal;\r
3044 BOOLEAN Hex;\r
3045\r
3046 Hex = FALSE;\r
3047\r
3048 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {\r
3049 Hex = TRUE;\r
125c2cf4 3050 }\r
252d9457 3051\r
3052 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
3053 return ((UINTN)RetVal);\r
3054 }\r
3055 return ((UINTN)(-1));\r
125c2cf4 3056}\r
3057\r
3058/**\r
3059 Safely append with automatic string resizing given length of Destination and\r
3060 desired length of copy from Source.\r
3061\r
3062 append the first D characters of Source to the end of Destination, where D is\r
3063 the lesser of Count and the StrLen() of Source. If appending those D characters\r
3064 will fit within Destination (whose Size is given as CurrentSize) and\r
1e6e84c7 3065 still leave room for a NULL terminator, then those characters are appended,\r
3066 starting at the original terminating NULL of Destination, and a new terminating\r
3067 NULL is appended.\r
125c2cf4 3068\r
3069 If appending D characters onto Destination will result in a overflow of the size\r
3070 given in CurrentSize the string will be grown such that the copy can be performed\r
3071 and CurrentSize will be updated to the new size.\r
3072\r
3073 If Source is NULL, there is nothing to append, just return the current buffer in\r
3074 Destination.\r
3075\r
3076 if Destination is NULL, then ASSERT()\r
3077 if Destination's current length (including NULL terminator) is already more then\r
3078 CurrentSize, then ASSERT()\r
3079\r
4ff7e37b
ED
3080 @param[in, out] Destination The String to append onto\r
3081 @param[in, out] CurrentSize on call the number of bytes in Destination. On\r
125c2cf4 3082 return possibly the new size (still in bytes). if NULL\r
3083 then allocate whatever is needed.\r
3084 @param[in] Source The String to append from\r
3085 @param[in] Count Maximum number of characters to append. if 0 then\r
3086 all are appended.\r
3087\r
3088 @return Destination return the resultant string.\r
3089**/\r
3090CHAR16*\r
3091EFIAPI\r
3092StrnCatGrow (\r
3093 IN OUT CHAR16 **Destination,\r
3094 IN OUT UINTN *CurrentSize,\r
3095 IN CONST CHAR16 *Source,\r
3096 IN UINTN Count\r
3097 )\r
3098{\r
3099 UINTN DestinationStartSize;\r
3100 UINTN NewSize;\r
3101\r
3102 //\r
3103 // ASSERTs\r
3104 //\r
3105 ASSERT(Destination != NULL);\r
3106\r
3107 //\r
3108 // If there's nothing to do then just return Destination\r
3109 //\r
3110 if (Source == NULL) {\r
3111 return (*Destination);\r
3112 }\r
3113\r
3114 //\r
3115 // allow for un-initialized pointers, based on size being 0\r
3116 //\r
3117 if (CurrentSize != NULL && *CurrentSize == 0) {\r
3118 *Destination = NULL;\r
3119 }\r
3120\r
3121 //\r
3122 // allow for NULL pointers address as Destination\r
3123 //\r
3124 if (*Destination != NULL) {\r
3125 ASSERT(CurrentSize != 0);\r
3126 DestinationStartSize = StrSize(*Destination);\r
3127 ASSERT(DestinationStartSize <= *CurrentSize);\r
3128 } else {\r
3129 DestinationStartSize = 0;\r
3130// ASSERT(*CurrentSize == 0);\r
3131 }\r
3132\r
3133 //\r
3134 // Append all of Source?\r
3135 //\r
3136 if (Count == 0) {\r
3137 Count = StrLen(Source);\r
3138 }\r
3139\r
3140 //\r
3141 // Test and grow if required\r
3142 //\r
3143 if (CurrentSize != NULL) {\r
3144 NewSize = *CurrentSize;\r
f480fdc0
ED
3145 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {\r
3146 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
3147 NewSize += 2 * Count * sizeof(CHAR16);\r
3148 }\r
3149 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
3150 *CurrentSize = NewSize;\r
125c2cf4 3151 }\r
125c2cf4 3152 } else {\r
3153 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
3154 }\r
3155\r
3156 //\r
3157 // Now use standard StrnCat on a big enough buffer\r
3158 //\r
c9d92df0 3159 if (*Destination == NULL) {\r
3160 return (NULL);\r
3161 }\r
125c2cf4 3162 return StrnCat(*Destination, Source, Count);\r
3163}\r
c9d92df0 3164\r
3165/**\r
3166 Prompt the user and return the resultant answer to the requestor.\r
3167\r
3168 This function will display the requested question on the shell prompt and then\r
3169 wait for an apropriate answer to be input from the console.\r
3170\r
a405b86d 3171 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
c9d92df0 3172 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3173\r
a405b86d 3174 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
c9d92df0 3175 CHAR16*.\r
3176\r
3177 In either case *Response must be callee freed if Response was not NULL;\r
3178\r
3179 @param Type What type of question is asked. This is used to filter the input\r
3180 to prevent invalid answers to question.\r
3181 @param Prompt Pointer to string prompt to use to request input.\r
3182 @param Response Pointer to Response which will be populated upon return.\r
3183\r
3184 @retval EFI_SUCCESS The operation was sucessful.\r
3185 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3186 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3187 @return other The operation failed.\r
3188**/\r
3189EFI_STATUS\r
3190EFIAPI\r
3191ShellPromptForResponse (\r
3192 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3193 IN CHAR16 *Prompt OPTIONAL,\r
3194 IN OUT VOID **Response OPTIONAL\r
3195 )\r
3196{\r
3197 EFI_STATUS Status;\r
3198 EFI_INPUT_KEY Key;\r
3199 UINTN EventIndex;\r
3200 SHELL_PROMPT_RESPONSE *Resp;\r
a405b86d 3201 UINTN Size;\r
3202 CHAR16 *Buffer;\r
c9d92df0 3203\r
a405b86d 3204 Status = EFI_UNSUPPORTED;\r
3205 Resp = NULL;\r
3206 Buffer = NULL;\r
3207 Size = 0;\r
3208 if (Type != ShellPromptResponseTypeFreeform) {\r
252d9457 3209 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
3e082d58 3210 if (Resp == NULL) {\r
3211 return (EFI_OUT_OF_RESOURCES);\r
3212 }\r
90bfa227 3213 }\r
c9d92df0 3214\r
3215 switch(Type) {\r
a405b86d 3216 case ShellPromptResponseTypeQuitContinue:\r
c9d92df0 3217 if (Prompt != NULL) {\r
3218 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3219 }\r
3220 //\r
3221 // wait for valid response\r
3222 //\r
3223 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3224 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3225 ASSERT_EFI_ERROR(Status);\r
3226 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3227 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r
a405b86d 3228 *Resp = ShellPromptResponseQuit;\r
c9d92df0 3229 } else {\r
a405b86d 3230 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3231 }\r
3232 break;\r
a405b86d 3233 case ShellPromptResponseTypeYesNoCancel:\r
c9d92df0 3234 if (Prompt != NULL) {\r
3235 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3236 }\r
3237 //\r
3238 // wait for valid response\r
3239 //\r
a405b86d 3240 *Resp = ShellPromptResponseMax;\r
3241 while (*Resp == ShellPromptResponseMax) {\r
c9d92df0 3242 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3243 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3244 ASSERT_EFI_ERROR(Status);\r
3245 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3246 switch (Key.UnicodeChar) {\r
3247 case L'Y':\r
3248 case L'y':\r
a405b86d 3249 *Resp = ShellPromptResponseYes;\r
c9d92df0 3250 break;\r
3251 case L'N':\r
3252 case L'n':\r
a405b86d 3253 *Resp = ShellPromptResponseNo;\r
3254 break;\r
3255 case L'C':\r
3256 case L'c':\r
3257 *Resp = ShellPromptResponseCancel;\r
3258 break;\r
3259 }\r
3260 }\r
3261 break; case ShellPromptResponseTypeYesNoAllCancel:\r
3262 if (Prompt != NULL) {\r
3263 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3264 }\r
3265 //\r
3266 // wait for valid response\r
3267 //\r
3268 *Resp = ShellPromptResponseMax;\r
3269 while (*Resp == ShellPromptResponseMax) {\r
3270 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3271 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3272 ASSERT_EFI_ERROR(Status);\r
3273 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3274 switch (Key.UnicodeChar) {\r
3275 case L'Y':\r
3276 case L'y':\r
3277 *Resp = ShellPromptResponseYes;\r
3278 break;\r
3279 case L'N':\r
3280 case L'n':\r
3281 *Resp = ShellPromptResponseNo;\r
c9d92df0 3282 break;\r
3283 case L'A':\r
3284 case L'a':\r
a405b86d 3285 *Resp = ShellPromptResponseAll;\r
c9d92df0 3286 break;\r
3287 case L'C':\r
3288 case L'c':\r
a405b86d 3289 *Resp = ShellPromptResponseCancel;\r
c9d92df0 3290 break;\r
3291 }\r
3292 }\r
3293 break;\r
a405b86d 3294 case ShellPromptResponseTypeEnterContinue:\r
3295 case ShellPromptResponseTypeAnyKeyContinue:\r
c9d92df0 3296 if (Prompt != NULL) {\r
3297 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3298 }\r
3299 //\r
3300 // wait for valid response\r
3301 //\r
a405b86d 3302 *Resp = ShellPromptResponseMax;\r
3303 while (*Resp == ShellPromptResponseMax) {\r
c9d92df0 3304 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
a405b86d 3305 if (Type == ShellPromptResponseTypeEnterContinue) {\r
c9d92df0 3306 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3307 ASSERT_EFI_ERROR(Status);\r
3308 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3309 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
a405b86d 3310 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3311 break;\r
3312 }\r
3313 }\r
a405b86d 3314 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3315 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3316 ASSERT_EFI_ERROR(Status);\r
3317 *Resp = ShellPromptResponseContinue;\r
3318 break;\r
3319 }\r
3320 }\r
3321 break;\r
3322 case ShellPromptResponseTypeYesNo:\r
3323 if (Prompt != NULL) {\r
3324 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3325 }\r
3326 //\r
3327 // wait for valid response\r
3328 //\r
3329 *Resp = ShellPromptResponseMax;\r
3330 while (*Resp == ShellPromptResponseMax) {\r
3331 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3332 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3333 ASSERT_EFI_ERROR(Status);\r
3334 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3335 switch (Key.UnicodeChar) {\r
3336 case L'Y':\r
3337 case L'y':\r
3338 *Resp = ShellPromptResponseYes;\r
3339 break;\r
3340 case L'N':\r
3341 case L'n':\r
3342 *Resp = ShellPromptResponseNo;\r
3343 break;\r
3344 }\r
3345 }\r
3346 break;\r
3347 case ShellPromptResponseTypeFreeform:\r
3348 if (Prompt != NULL) {\r
3349 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3350 }\r
3351 while(1) {\r
3352 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3353 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3354 ASSERT_EFI_ERROR(Status);\r
3355 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3356 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
c9d92df0 3357 break;\r
3358 }\r
a405b86d 3359 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3360 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);\r
c9d92df0 3361 }\r
3362 break;\r
a405b86d 3363 //\r
3364 // This is the location to add new prompt types.\r
3365 //\r
c9d92df0 3366 default:\r
a405b86d 3367 ASSERT(FALSE);\r
c9d92df0 3368 }\r
3369\r
3370 if (Response != NULL) {\r
a405b86d 3371 if (Resp != NULL) {\r
3372 *Response = Resp;\r
3373 } else if (Buffer != NULL) {\r
3374 *Response = Buffer;\r
3375 }\r
c9d92df0 3376 } else {\r
a405b86d 3377 if (Resp != NULL) {\r
3378 FreePool(Resp);\r
3379 }\r
3380 if (Buffer != NULL) {\r
3381 FreePool(Buffer);\r
3382 }\r
c9d92df0 3383 }\r
3384\r
a405b86d 3385 ShellPrintEx(-1, -1, L"\r\n");\r
c9d92df0 3386 return (Status);\r
3387}\r
3388\r
3389/**\r
3390 Prompt the user and return the resultant answer to the requestor.\r
3391\r
3392 This function is the same as ShellPromptForResponse, except that the prompt is\r
3393 automatically pulled from HII.\r
3394\r
3395 @param Type What type of question is asked. This is used to filter the input\r
3396 to prevent invalid answers to question.\r
a405b86d 3397 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3398 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3399 @param Response Pointer to Response which will be populated upon return.\r
c9d92df0 3400\r
3401 @retval EFI_SUCCESS the operation was sucessful.\r
3402 @return other the operation failed.\r
3403\r
3404 @sa ShellPromptForResponse\r
3405**/\r
3406EFI_STATUS\r
3407EFIAPI\r
3408ShellPromptForResponseHii (\r
3409 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3410 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3411 IN CONST EFI_HANDLE HiiFormatHandle,\r
3412 IN OUT VOID **Response\r
3413 )\r
3414{\r
3415 CHAR16 *Prompt;\r
3416 EFI_STATUS Status;\r
3417\r
3418 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
3419 Status = ShellPromptForResponse(Type, Prompt, Response);\r
3420 FreePool(Prompt);\r
3421 return (Status);\r
3422}\r
3423\r
a405b86d 3424/**\r
3425 Function to determin if an entire string is a valid number.\r
3426\r
3427 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
c9d92df0 3428\r
a405b86d 3429 @param[in] String The string to evaluate.\r
3430 @param[in] ForceHex TRUE - always assume hex.\r
3431 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3432\r
3433 @retval TRUE It is all numeric (dec/hex) characters.\r
3434 @retval FALSE There is a non-numeric character.\r
3435**/\r
3436BOOLEAN\r
3437EFIAPI\r
252d9457 3438InternalShellIsHexOrDecimalNumber (\r
a405b86d 3439 IN CONST CHAR16 *String,\r
3440 IN CONST BOOLEAN ForceHex,\r
3441 IN CONST BOOLEAN StopAtSpace\r
3442 )\r
3443{\r
3444 BOOLEAN Hex;\r
3445\r
3446 //\r
3447 // chop off a single negative sign\r
3448 //\r
3449 if (String != NULL && *String == L'-') {\r
3450 String++;\r
3451 }\r
b0934ac4 3452\r
a405b86d 3453 if (String == NULL) {\r
3454 return (FALSE);\r
3455 }\r
3456\r
3457 //\r
3458 // chop leading zeroes\r
3459 //\r
3460 while(String != NULL && *String == L'0'){\r
3461 String++;\r
3462 }\r
3463 //\r
3464 // allow '0x' or '0X', but not 'x' or 'X'\r
3465 //\r
3466 if (String != NULL && (*String == L'x' || *String == L'X')) {\r
3467 if (*(String-1) != L'0') {\r
3468 //\r
3469 // we got an x without a preceeding 0\r
3470 //\r
3471 return (FALSE);\r
3472 }\r
3473 String++;\r
3474 Hex = TRUE;\r
3475 } else if (ForceHex) {\r
3476 Hex = TRUE;\r
3477 } else {\r
3478 Hex = FALSE;\r
3479 }\r
3480\r
3481 //\r
3482 // loop through the remaining characters and use the lib function\r
3483 //\r
3484 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
3485 if (Hex) {\r
3486 if (!ShellIsHexaDecimalDigitCharacter(*String)) {\r
3487 return (FALSE);\r
3488 }\r
3489 } else {\r
3490 if (!ShellIsDecimalDigitCharacter(*String)) {\r
3491 return (FALSE);\r
3492 }\r
3493 }\r
3494 }\r
252d9457 3495\r
a405b86d 3496 return (TRUE);\r
3497}\r
3498\r
3499/**\r
3500 Function to determine if a given filename exists.\r
3501\r
3502 @param[in] Name Path to test.\r
3503\r
3504 @retval EFI_SUCCESS The Path represents a file.\r
3505 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3506 @retval other The path failed to open.\r
3507**/\r
3508EFI_STATUS\r
3509EFIAPI\r
3510ShellFileExists(\r
3511 IN CONST CHAR16 *Name\r
3512 )\r
3513{\r
3514 EFI_STATUS Status;\r
3515 EFI_SHELL_FILE_INFO *List;\r
3516\r
3517 ASSERT(Name != NULL);\r
3518\r
3519 List = NULL;\r
3520 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);\r
3521 if (EFI_ERROR(Status)) {\r
3522 return (Status);\r
3523 }\r
3524\r
3525 ShellCloseFileMetaArg(&List);\r
3526\r
3527 return (EFI_SUCCESS);\r
3528}\r
252d9457 3529\r
3530/**\r
b0934ac4 3531 Convert a Unicode character to upper case only if\r
252d9457 3532 it maps to a valid small-case ASCII character.\r
3533\r
3534 This internal function only deal with Unicode character\r
3535 which maps to a valid small-case ASCII character, i.e.\r
3536 L'a' to L'z'. For other Unicode character, the input character\r
3537 is returned directly.\r
3538\r
3539 @param Char The character to convert.\r
3540\r
3541 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
3542 @retval Unchanged Otherwise.\r
3543\r
3544**/\r
3545CHAR16\r
3546EFIAPI\r
3547InternalShellCharToUpper (\r
3548 IN CHAR16 Char\r
3549 )\r
3550{\r
3551 if (Char >= L'a' && Char <= L'z') {\r
3552 return (CHAR16) (Char - (L'a' - L'A'));\r
3553 }\r
3554\r
3555 return Char;\r
3556}\r
3557\r
3558/**\r
3559 Convert a Unicode character to numerical value.\r
3560\r
3561 This internal function only deal with Unicode character\r
3562 which maps to a valid hexadecimal ASII character, i.e.\r
b0934ac4 3563 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other\r
252d9457 3564 Unicode character, the value returned does not make sense.\r
3565\r
3566 @param Char The character to convert.\r
3567\r
3568 @return The numerical value converted.\r
3569\r
3570**/\r
3571UINTN\r
3572EFIAPI\r
3573InternalShellHexCharToUintn (\r
3574 IN CHAR16 Char\r
3575 )\r
3576{\r
3577 if (ShellIsDecimalDigitCharacter (Char)) {\r
3578 return Char - L'0';\r
3579 }\r
3580\r
3581 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
3582}\r
3583\r
3584/**\r
3585 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3586\r
3587 This function returns a value of type UINTN by interpreting the contents\r
3588 of the Unicode string specified by String as a hexadecimal number.\r
3589 The format of the input Unicode string String is:\r
3590\r
3591 [spaces][zeros][x][hexadecimal digits].\r
3592\r
3593 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3594 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3595 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3596 The function will ignore the pad space, which includes spaces or tab characters,\r
3597 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3598 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3599 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3600 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3601\r
3602 If String has only pad spaces, then zero is returned.\r
3603 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3604 then zero is returned.\r
3605\r
3606 @param[in] String A pointer to a Null-terminated Unicode string.\r
3607 @param[out] Value Upon a successful return the value of the conversion.\r
3608 @param[in] StopAtSpace FALSE to skip spaces.\r
3609\r
3610 @retval EFI_SUCCESS The conversion was successful.\r
3611 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3612 @retval EFI_DEVICE_ERROR An overflow occured.\r
3613**/\r
3614EFI_STATUS\r
3615EFIAPI\r
3616InternalShellStrHexToUint64 (\r
3617 IN CONST CHAR16 *String,\r
3618 OUT UINT64 *Value,\r
3619 IN CONST BOOLEAN StopAtSpace\r
3620 )\r
3621{\r
3622 UINT64 Result;\r
3623\r
3624 if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
3625 return (EFI_INVALID_PARAMETER);\r
3626 }\r
b0934ac4 3627\r
252d9457 3628 //\r
b0934ac4 3629 // Ignore the pad spaces (space or tab)\r
252d9457 3630 //\r
3631 while ((*String == L' ') || (*String == L'\t')) {\r
3632 String++;\r
3633 }\r
3634\r
3635 //\r
3636 // Ignore leading Zeros after the spaces\r
3637 //\r
3638 while (*String == L'0') {\r
3639 String++;\r
3640 }\r
3641\r
3642 if (InternalShellCharToUpper (*String) == L'X') {\r
3643 if (*(String - 1) != L'0') {\r
3644 return 0;\r
3645 }\r
3646 //\r
3647 // Skip the 'X'\r
3648 //\r
3649 String++;\r
3650 }\r
3651\r
3652 Result = 0;\r
b0934ac4 3653\r
252d9457 3654 //\r
12ea4694 3655 // Skip spaces if requested\r
252d9457 3656 //\r
12ea4694
ED
3657 while (StopAtSpace && *String == L' ') {\r
3658 String++;\r
252d9457 3659 }\r
b0934ac4 3660\r
252d9457 3661 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
3662 //\r
b0934ac4 3663 // If the Hex Number represented by String overflows according\r
252d9457 3664 // to the range defined by UINTN, then ASSERT().\r
3665 //\r
3666 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
3667// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
3668 return (EFI_DEVICE_ERROR);\r
3669 }\r
3670\r
3671 Result = (LShiftU64(Result, 4));\r
3672 Result += InternalShellHexCharToUintn (*String);\r
3673 String++;\r
3674\r
3675 //\r
49bd498d 3676 // stop at spaces if requested\r
252d9457 3677 //\r
49bd498d 3678 if (StopAtSpace && *String == L' ') {\r
3679 break;\r
252d9457 3680 }\r
3681 }\r
3682\r
3683 *Value = Result;\r
3684 return (EFI_SUCCESS);\r
3685}\r
3686\r
3687/**\r
3688 Convert a Null-terminated Unicode decimal string to a value of\r
3689 type UINT64.\r
3690\r
3691 This function returns a value of type UINT64 by interpreting the contents\r
3692 of the Unicode string specified by String as a decimal number. The format\r
3693 of the input Unicode string String is:\r
3694\r
3695 [spaces] [decimal digits].\r
3696\r
3697 The valid decimal digit character is in the range [0-9]. The\r
3698 function will ignore the pad space, which includes spaces or\r
3699 tab characters, before [decimal digits]. The running zero in the\r
3700 beginning of [decimal digits] will be ignored. Then, the function\r
3701 stops at the first character that is a not a valid decimal character\r
3702 or a Null-terminator, whichever one comes first.\r
3703\r
3704 If String has only pad spaces, then 0 is returned.\r
3705 If String has no pad spaces or valid decimal digits,\r
3706 then 0 is returned.\r
3707\r
3708 @param[in] String A pointer to a Null-terminated Unicode string.\r
3709 @param[out] Value Upon a successful return the value of the conversion.\r
3710 @param[in] StopAtSpace FALSE to skip spaces.\r
3711\r
3712 @retval EFI_SUCCESS The conversion was successful.\r
3713 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3714 @retval EFI_DEVICE_ERROR An overflow occured.\r
3715**/\r
3716EFI_STATUS\r
3717EFIAPI\r
3718InternalShellStrDecimalToUint64 (\r
3719 IN CONST CHAR16 *String,\r
3720 OUT UINT64 *Value,\r
3721 IN CONST BOOLEAN StopAtSpace\r
3722 )\r
3723{\r
3724 UINT64 Result;\r
3725\r
3726 if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
3727 return (EFI_INVALID_PARAMETER);\r
3728 }\r
3729\r
3730 //\r
3731 // Ignore the pad spaces (space or tab)\r
3732 //\r
3733 while ((*String == L' ') || (*String == L'\t')) {\r
3734 String++;\r
3735 }\r
3736\r
3737 //\r
3738 // Ignore leading Zeros after the spaces\r
3739 //\r
3740 while (*String == L'0') {\r
3741 String++;\r
3742 }\r
3743\r
3744 Result = 0;\r
3745\r
3746 //\r
3747 // Skip spaces if requested\r
3748 //\r
3749 while (StopAtSpace && *String == L' ') {\r
3750 String++;\r
3751 }\r
3752 while (ShellIsDecimalDigitCharacter (*String)) {\r
3753 //\r
b0934ac4 3754 // If the number represented by String overflows according\r
252d9457 3755 // to the range defined by UINT64, then ASSERT().\r
3756 //\r
b0934ac4 3757\r
252d9457 3758 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
3759 return (EFI_DEVICE_ERROR);\r
3760 }\r
3761\r
3762 Result = MultU64x32(Result, 10) + (*String - L'0');\r
3763 String++;\r
3764\r
3765 //\r
3766 // Stop at spaces if requested\r
3767 //\r
3768 if (StopAtSpace && *String == L' ') {\r
3769 break;\r
3770 }\r
3771 }\r
3772\r
3773 *Value = Result;\r
b0934ac4 3774\r
252d9457 3775 return (EFI_SUCCESS);\r
3776}\r
3777\r
3778/**\r
3779 Function to verify and convert a string to its numerical value.\r
3780\r
3781 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
3782\r
3783 @param[in] String The string to evaluate.\r
3784 @param[out] Value Upon a successful return the value of the conversion.\r
3785 @param[in] ForceHex TRUE - always assume hex.\r
3786 @param[in] StopAtSpace FALSE to skip spaces.\r
b0934ac4 3787\r
252d9457 3788 @retval EFI_SUCCESS The conversion was successful.\r
3789 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
3790 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
3791**/\r
3792EFI_STATUS\r
3793EFIAPI\r
3794ShellConvertStringToUint64(\r
3795 IN CONST CHAR16 *String,\r
3796 OUT UINT64 *Value,\r
3797 IN CONST BOOLEAN ForceHex,\r
3798 IN CONST BOOLEAN StopAtSpace\r
3799 )\r
3800{\r
3801 UINT64 RetVal;\r
3802 CONST CHAR16 *Walker;\r
3803 EFI_STATUS Status;\r
3804 BOOLEAN Hex;\r
3805\r
3806 Hex = ForceHex;\r
3807\r
3808 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3809 if (!Hex) {\r
3810 Hex = TRUE;\r
3811 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3812 return (EFI_INVALID_PARAMETER);\r
3813 }\r
3814 } else {\r
3815 return (EFI_INVALID_PARAMETER);\r
3816 }\r
3817 }\r
3818\r
3819 //\r
3820 // Chop off leading spaces\r
3821 //\r
3822 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
3823\r
3824 //\r
3825 // make sure we have something left that is numeric.\r
3826 //\r
3827 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {\r
3828 return (EFI_INVALID_PARAMETER);\r
b0934ac4 3829 }\r
252d9457 3830\r
3831 //\r
3832 // do the conversion.\r
3833 //\r
3834 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
3835 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
3836 } else {\r
3837 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
3838 }\r
3839\r
3840 if (Value == NULL && !EFI_ERROR(Status)) {\r
3841 return (EFI_NOT_FOUND);\r
3842 }\r
3843\r
3844 if (Value != NULL) {\r
3845 *Value = RetVal;\r
3846 }\r
3847\r
3848 return (Status);\r
3849}\r
3850\r
3851/**\r
3852 Function to determin if an entire string is a valid number.\r
3853\r
3854 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3855\r
3856 @param[in] String The string to evaluate.\r
3857 @param[in] ForceHex TRUE - always assume hex.\r
3858 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3859\r
3860 @retval TRUE It is all numeric (dec/hex) characters.\r
3861 @retval FALSE There is a non-numeric character.\r
3862**/\r
3863BOOLEAN\r
3864EFIAPI\r
3865ShellIsHexOrDecimalNumber (\r
3866 IN CONST CHAR16 *String,\r
3867 IN CONST BOOLEAN ForceHex,\r
3868 IN CONST BOOLEAN StopAtSpace\r
3869 )\r
3870{\r
3871 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
3872 return (TRUE);\r
3873 }\r
3874 return (FALSE);\r
3875}\r
4d0a4fce 3876\r
3877/**\r
3878 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
3879 buffer. The returned buffer must be callee freed.\r
3880\r
3881 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
3882 maintained and not changed for all operations with the same file.\r
3883\r
4ff7e37b
ED
3884 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
3885 @param[in, out] Ascii Boolean value for indicating whether the file is\r
3886 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 3887\r
beab0fc5 3888 @return The line of text from the file.\r
3889 @retval NULL There was not enough memory available.\r
4d0a4fce 3890\r
3891 @sa ShellFileHandleReadLine\r
3892**/\r
3893CHAR16*\r
3894EFIAPI\r
3895ShellFileHandleReturnLine(\r
3896 IN SHELL_FILE_HANDLE Handle,\r
3897 IN OUT BOOLEAN *Ascii\r
3898 )\r
3899{\r
3900 CHAR16 *RetVal;\r
3901 UINTN Size;\r
3902 EFI_STATUS Status;\r
3903\r
3904 Size = 0;\r
3905 RetVal = NULL;\r
3906\r
3907 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
3908 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3909 RetVal = AllocateZeroPool(Size);\r
beab0fc5 3910 if (RetVal == NULL) {\r
3911 return (NULL);\r
3912 }\r
4d0a4fce 3913 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
b0934ac4 3914\r
4d0a4fce 3915 }\r
4d0a4fce 3916 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
3917 FreePool(RetVal);\r
3918 RetVal = NULL;\r
3919 }\r
3920 return (RetVal);\r
3921}\r
3922\r
3923/**\r
3924 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
3925\r
3926 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
3927 maintained and not changed for all operations with the same file.\r
3928\r
4ff7e37b
ED
3929 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
3930 @param[in, out] Buffer The pointer to buffer to read into.\r
3931 @param[in, out] Size The pointer to number of bytes in Buffer.\r
3932 @param[in] Truncate If the buffer is large enough, this has no effect.\r
3933 If the buffer is is too small and Truncate is TRUE,\r
3934 the line will be truncated.\r
3935 If the buffer is is too small and Truncate is FALSE,\r
3936 then no read will occur.\r
3937\r
3938 @param[in, out] Ascii Boolean value for indicating whether the file is\r
3939 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 3940\r
3941 @retval EFI_SUCCESS The operation was successful. The line is stored in\r
3942 Buffer.\r
3943 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
3944 @retval EFI_INVALID_PARAMETER Size was NULL.\r
3945 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
3946 Size was updated to the minimum space required.\r
3947**/\r
3948EFI_STATUS\r
3949EFIAPI\r
3950ShellFileHandleReadLine(\r
3951 IN SHELL_FILE_HANDLE Handle,\r
3952 IN OUT CHAR16 *Buffer,\r
3953 IN OUT UINTN *Size,\r
3954 IN BOOLEAN Truncate,\r
3955 IN OUT BOOLEAN *Ascii\r
3956 )\r
3957{\r
3958 EFI_STATUS Status;\r
3959 CHAR16 CharBuffer;\r
3960 UINTN CharSize;\r
3961 UINTN CountSoFar;\r
3962 UINT64 OriginalFilePosition;\r
3963\r
3964\r
3965 if (Handle == NULL\r
3966 ||Size == NULL\r
3967 ){\r
3968 return (EFI_INVALID_PARAMETER);\r
3969 }\r
3970 if (Buffer == NULL) {\r
3971 ASSERT(*Size == 0);\r
3972 } else {\r
3973 *Buffer = CHAR_NULL;\r
3974 }\r
3975 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
3976 if (OriginalFilePosition == 0) {\r
3977 CharSize = sizeof(CHAR16);\r
3978 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
3979 ASSERT_EFI_ERROR(Status);\r
3980 if (CharBuffer == gUnicodeFileTag) {\r
3981 *Ascii = FALSE;\r
3982 } else {\r
3983 *Ascii = TRUE;\r
3984 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
3985 }\r
3986 }\r
3987\r
3988 for (CountSoFar = 0;;CountSoFar++){\r
3989 CharBuffer = 0;\r
3990 if (*Ascii) {\r
3991 CharSize = sizeof(CHAR8);\r
3992 } else {\r
3993 CharSize = sizeof(CHAR16);\r
3994 }\r
3995 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
3996 if ( EFI_ERROR(Status)\r
3997 || CharSize == 0\r
3998 || (CharBuffer == L'\n' && !(*Ascii))\r
3999 || (CharBuffer == '\n' && *Ascii)\r
4000 ){\r
4001 break;\r
4002 }\r
4003 //\r
4004 // if we have space save it...\r
4005 //\r
4006 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
4007 ASSERT(Buffer != NULL);\r
4008 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
4009 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
4010 }\r
4011 }\r
4012\r
4013 //\r
4014 // if we ran out of space tell when...\r
4015 //\r
4016 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
4017 *Size = (CountSoFar+1)*sizeof(CHAR16);\r
4018 if (!Truncate) {\r
4019 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
4020 } else {\r
4021 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
4022 }\r
4023 return (EFI_BUFFER_TOO_SMALL);\r
4024 }\r
4025 while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
4026 Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
4027 }\r
4028\r
4029 return (Status);\r
4030}\r