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