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