]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
updating comments mostly. also added some new lib functions.
[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
b3011f40 4 Copyright (c) 2006 - 2010, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
94b17fa1 9\r
b3011f40 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
94b17fa1 12\r
13**/\r
14\r
b1f95a06 15#include "UefiShellLib.h"\r
d2b4564b 16\r
94b17fa1 17#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
18#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
19\r
d2b4564b 20//\r
21// This is not static since it's extern in the .h file\r
22//\r
23SHELL_PARAM_ITEM EmptyParamList[] = {\r
24 {NULL, TypeMax}\r
25 };\r
26\r
27//\r
28// Static file globals for the shell library\r
29//\r
30STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
31STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;\r
32STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;\r
33STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r
34STATIC EFI_HANDLE mEfiShellEnvironment2Handle;\r
35STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
2247dde4 36STATIC UINTN mTotalParameterCount;\r
ecd3d59f 37STATIC CHAR16 *mPostReplaceFormat;\r
38STATIC CHAR16 *mPostReplaceFormat2;\r
b3011f40 39\r
2247dde4 40/**\r
41 Check if a Unicode character is a hexadecimal character.\r
42\r
43 This internal function checks if a Unicode character is a \r
44 decimal character. The valid hexadecimal character is \r
45 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
46\r
47\r
48 @param Char The character to check against.\r
49\r
50 @retval TRUE If the Char is a hexadecmial character.\r
51 @retval FALSE If the Char is not a hexadecmial character.\r
52\r
53**/\r
54BOOLEAN\r
55EFIAPI\r
b3011f40 56ShellLibIsHexaDecimalDigitCharacter (\r
2247dde4 57 IN CHAR16 Char\r
58 ) {\r
59 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
60}\r
94b17fa1 61\r
62/**\r
63 helper function to find ShellEnvironment2 for constructor\r
64**/\r
65EFI_STATUS\r
66EFIAPI\r
67ShellFindSE2 (\r
68 IN EFI_HANDLE ImageHandle\r
2247dde4 69 ) {\r
94b17fa1 70 EFI_STATUS Status;\r
71 EFI_HANDLE *Buffer;\r
72 UINTN BufferSize;\r
73 UINTN HandleIndex;\r
74\r
75 BufferSize = 0;\r
76 Buffer = NULL;\r
77 Status = gBS->OpenProtocol(ImageHandle, \r
78 &gEfiShellEnvironment2Guid,\r
79 (VOID **)&mEfiShellEnvironment2,\r
80 ImageHandle,\r
81 NULL,\r
82 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
83 );\r
84 //\r
85 // look for the mEfiShellEnvironment2 protocol at a higher level\r
86 //\r
9eb53ac3 87 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){\r
94b17fa1 88 //\r
89 // figure out how big of a buffer we need.\r
90 //\r
91 Status = gBS->LocateHandle (ByProtocol,\r
92 &gEfiShellEnvironment2Guid,\r
93 NULL, // ignored for ByProtocol\r
94 &BufferSize,\r
95 Buffer\r
96 );\r
2247dde4 97 //\r
98 // maybe it's not there???\r
99 //\r
100 if (Status == EFI_BUFFER_TOO_SMALL) {\r
101 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);\r
102 ASSERT(Buffer != NULL);\r
103 Status = gBS->LocateHandle (ByProtocol,\r
104 &gEfiShellEnvironment2Guid,\r
105 NULL, // ignored for ByProtocol\r
106 &BufferSize,\r
107 Buffer\r
108 );\r
109 }\r
94b17fa1 110 if (!EFI_ERROR (Status)) {\r
111 //\r
112 // now parse the list of returned handles\r
113 //\r
114 Status = EFI_NOT_FOUND;\r
115 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r
116 Status = gBS->OpenProtocol(Buffer[HandleIndex], \r
117 &gEfiShellEnvironment2Guid,\r
118 (VOID **)&mEfiShellEnvironment2,\r
119 ImageHandle,\r
120 NULL,\r
121 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
122 );\r
9eb53ac3 123 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {\r
94b17fa1 124 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
125 Status = EFI_SUCCESS;\r
126 break;\r
127 }\r
128 }\r
129 }\r
130 }\r
131 if (Buffer != NULL) {\r
132 FreePool (Buffer);\r
133 }\r
134 return (Status);\r
135}\r
136\r
94b17fa1 137EFI_STATUS\r
138EFIAPI\r
d2b4564b 139ShellLibConstructorWorker (\r
94b17fa1 140 IN EFI_HANDLE ImageHandle,\r
141 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 142 ) {\r
94b17fa1 143 EFI_STATUS Status;\r
144\r
b3011f40 145 ASSERT(PcdGet16 (PcdShellPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
146 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
ecd3d59f 147 ASSERT (mPostReplaceFormat != NULL);\r
b3011f40 148 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
ecd3d59f 149 ASSERT (mPostReplaceFormat2 != NULL);\r
150\r
2247dde4 151 //\r
152 // Set the parameter count to an invalid number\r
153 //\r
154 mTotalParameterCount = (UINTN)(-1);\r
155\r
94b17fa1 156 //\r
157 // UEFI 2.0 shell interfaces (used preferentially)\r
158 //\r
159 Status = gBS->OpenProtocol(ImageHandle, \r
160 &gEfiShellProtocolGuid,\r
161 (VOID **)&mEfiShellProtocol,\r
162 ImageHandle,\r
163 NULL,\r
164 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
165 );\r
166 if (EFI_ERROR(Status)) {\r
167 mEfiShellProtocol = NULL;\r
168 }\r
169 Status = gBS->OpenProtocol(ImageHandle, \r
170 &gEfiShellParametersProtocolGuid,\r
171 (VOID **)&mEfiShellParametersProtocol,\r
172 ImageHandle,\r
173 NULL,\r
174 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
175 );\r
176 if (EFI_ERROR(Status)) {\r
177 mEfiShellParametersProtocol = NULL;\r
178 }\r
179\r
180 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r
181 //\r
182 // Moved to seperate function due to complexity\r
183 //\r
184 Status = ShellFindSE2(ImageHandle);\r
185\r
186 if (EFI_ERROR(Status)) {\r
187 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
188 mEfiShellEnvironment2 = NULL;\r
189 }\r
190 Status = gBS->OpenProtocol(ImageHandle, \r
191 &gEfiShellInterfaceGuid,\r
192 (VOID **)&mEfiShellInterface,\r
193 ImageHandle,\r
194 NULL,\r
195 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
196 );\r
197 if (EFI_ERROR(Status)) {\r
198 mEfiShellInterface = NULL;\r
199 }\r
200 }\r
201 //\r
202 // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
203 //\r
204 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) || \r
205 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r
d2b4564b 206 if (mEfiShellProtocol != NULL) {\r
207 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r
208 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r
209 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r
210 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r
211 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r
212 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r
213 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r
214 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r
215 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r
216 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r
217 } else {\r
218 FileFunctionMap.GetFileInfo = FileHandleGetInfo;\r
219 FileFunctionMap.SetFileInfo = FileHandleSetInfo;\r
220 FileFunctionMap.ReadFile = FileHandleRead;\r
221 FileFunctionMap.WriteFile = FileHandleWrite;\r
222 FileFunctionMap.CloseFile = FileHandleClose;\r
223 FileFunctionMap.DeleteFile = FileHandleDelete;\r
224 FileFunctionMap.GetFilePosition = FileHandleGetPosition;\r
225 FileFunctionMap.SetFilePosition = FileHandleSetPosition;\r
226 FileFunctionMap.FlushFile = FileHandleFlush;\r
227 FileFunctionMap.GetFileSize = FileHandleGetSize;\r
228 }\r
94b17fa1 229 return (EFI_SUCCESS);\r
230 }\r
231 return (EFI_NOT_FOUND);\r
232}\r
d2b4564b 233/**\r
234 Constructor for the Shell library.\r
235\r
236 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
237\r
238 @param ImageHandle the image handle of the process\r
239 @param SystemTable the EFI System Table pointer\r
240\r
241 @retval EFI_SUCCESS the initialization was complete sucessfully\r
242 @return others an error ocurred during initialization\r
243**/\r
244EFI_STATUS\r
245EFIAPI\r
246ShellLibConstructor (\r
247 IN EFI_HANDLE ImageHandle,\r
248 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 249 ) {\r
d2b4564b 250\r
251\r
252 mEfiShellEnvironment2 = NULL;\r
253 mEfiShellProtocol = NULL;\r
254 mEfiShellParametersProtocol = NULL;\r
255 mEfiShellInterface = NULL;\r
256 mEfiShellEnvironment2Handle = NULL;\r
ecd3d59f 257 mPostReplaceFormat = NULL;\r
258 mPostReplaceFormat2 = NULL;\r
d2b4564b 259\r
d2b4564b 260 //\r
261 // verify that auto initialize is not set false\r
262 // \r
263 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r
264 return (EFI_SUCCESS);\r
265 }\r
266 \r
267 return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r
268}\r
94b17fa1 269\r
270/**\r
271 Destructory for the library. free any resources.\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275ShellLibDestructor (\r
276 IN EFI_HANDLE ImageHandle,\r
277 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 278 ) {\r
94b17fa1 279 if (mEfiShellEnvironment2 != NULL) {\r
280 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
281 &gEfiShellEnvironment2Guid,\r
282 ImageHandle,\r
283 NULL);\r
d2b4564b 284 mEfiShellEnvironment2 = NULL;\r
94b17fa1 285 }\r
286 if (mEfiShellInterface != NULL) {\r
287 gBS->CloseProtocol(ImageHandle,\r
288 &gEfiShellInterfaceGuid,\r
289 ImageHandle,\r
290 NULL); \r
d2b4564b 291 mEfiShellInterface = NULL;\r
94b17fa1 292 }\r
293 if (mEfiShellProtocol != NULL) {\r
294 gBS->CloseProtocol(ImageHandle,\r
295 &gEfiShellProtocolGuid,\r
296 ImageHandle,\r
d2b4564b 297 NULL); \r
298 mEfiShellProtocol = NULL;\r
94b17fa1 299 }\r
300 if (mEfiShellParametersProtocol != NULL) {\r
301 gBS->CloseProtocol(ImageHandle,\r
302 &gEfiShellParametersProtocolGuid,\r
303 ImageHandle,\r
304 NULL); \r
d2b4564b 305 mEfiShellParametersProtocol = NULL;\r
94b17fa1 306 }\r
d2b4564b 307 mEfiShellEnvironment2Handle = NULL;\r
ecd3d59f 308\r
309 if (mPostReplaceFormat != NULL) {\r
310 FreePool(mPostReplaceFormat);\r
311 }\r
312 if (mPostReplaceFormat2 != NULL) {\r
313 FreePool(mPostReplaceFormat2);\r
314 }\r
315 mPostReplaceFormat = NULL;\r
316 mPostReplaceFormat2 = NULL;\r
317\r
94b17fa1 318 return (EFI_SUCCESS);\r
319}\r
d2b4564b 320\r
321/**\r
322 This function causes the shell library to initialize itself. If the shell library\r
323 is already initialized it will de-initialize all the current protocol poitners and\r
324 re-populate them again.\r
325\r
326 When the library is used with PcdShellLibAutoInitialize set to true this function\r
327 will return EFI_SUCCESS and perform no actions.\r
328\r
329 This function is intended for internal access for shell commands only.\r
330\r
331 @retval EFI_SUCCESS the initialization was complete sucessfully\r
332\r
333**/\r
334EFI_STATUS\r
335EFIAPI\r
336ShellInitialize (\r
337 ) {\r
338 //\r
339 // if auto initialize is not false then skip\r
340 //\r
341 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {\r
342 return (EFI_SUCCESS);\r
343 }\r
344\r
345 //\r
346 // deinit the current stuff\r
347 //\r
348 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r
349\r
350 //\r
351 // init the new stuff\r
352 //\r
353 return (ShellLibConstructorWorker(gImageHandle, gST));\r
354}\r
355\r
94b17fa1 356/**\r
357 This function will retrieve the information about the file for the handle \r
358 specified and store it in allocated pool memory.\r
359\r
69817bf8 360 This function allocates a buffer to store the file's information. It is the \r
361 caller's responsibility to free the buffer\r
94b17fa1 362\r
363 @param FileHandle The file handle of the file for which information is \r
364 being requested.\r
365\r
366 @retval NULL information could not be retrieved.\r
367\r
368 @return the information about the file\r
369**/\r
370EFI_FILE_INFO*\r
371EFIAPI\r
372ShellGetFileInfo (\r
373 IN EFI_FILE_HANDLE FileHandle\r
2247dde4 374 ) {\r
d2b4564b 375 return (FileFunctionMap.GetFileInfo(FileHandle));\r
94b17fa1 376}\r
377\r
378/**\r
379 This function will set the information about the file for the opened handle \r
380 specified.\r
381\r
382 @param FileHandle The file handle of the file for which information \r
383 is being set\r
384\r
385 @param FileInfo The infotmation to set.\r
386\r
387 @retval EFI_SUCCESS The information was set.\r
388 @retval EFI_UNSUPPORTED The InformationType is not known.\r
389 @retval EFI_NO_MEDIA The device has no medium.\r
390 @retval EFI_DEVICE_ERROR The device reported an error.\r
391 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
392 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
393 @retval EFI_ACCESS_DENIED The file was opened read only.\r
394 @retval EFI_VOLUME_FULL The volume is full.\r
395**/\r
396EFI_STATUS\r
397EFIAPI\r
398ShellSetFileInfo (\r
399 IN EFI_FILE_HANDLE FileHandle,\r
400 IN EFI_FILE_INFO *FileInfo\r
2247dde4 401 ) {\r
d2b4564b 402 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r
94b17fa1 403} \r
404 \r
405 /**\r
406 This function will open a file or directory referenced by DevicePath.\r
407\r
408 This function opens a file with the open mode according to the file path. The \r
409 Attributes is valid only for EFI_FILE_MODE_CREATE.\r
410\r
411 @param FilePath on input the device path to the file. On output \r
412 the remaining device path.\r
413 @param DeviceHandle pointer to the system device handle.\r
414 @param FileHandle pointer to the file handle.\r
415 @param OpenMode the mode to open the file with.\r
416 @param Attributes the file's file attributes.\r
417\r
418 @retval EFI_SUCCESS The information was set.\r
419 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
420 @retval EFI_UNSUPPORTED Could not open the file path. \r
421 @retval EFI_NOT_FOUND The specified file could not be found on the \r
422 device or the file system could not be found on \r
423 the device.\r
424 @retval EFI_NO_MEDIA The device has no medium.\r
425 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
426 medium is no longer supported.\r
427 @retval EFI_DEVICE_ERROR The device reported an error.\r
428 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
429 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
430 @retval EFI_ACCESS_DENIED The file was opened read only.\r
431 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
432 file.\r
433 @retval EFI_VOLUME_FULL The volume is full.\r
434**/\r
435EFI_STATUS\r
436EFIAPI\r
437ShellOpenFileByDevicePath(\r
438 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
439 OUT EFI_HANDLE *DeviceHandle,\r
440 OUT EFI_FILE_HANDLE *FileHandle,\r
441 IN UINT64 OpenMode,\r
442 IN UINT64 Attributes\r
2247dde4 443 ) {\r
94b17fa1 444 CHAR16 *FileName;\r
445 EFI_STATUS Status;\r
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
447 EFI_FILE_HANDLE LastHandle;\r
448\r
449 //\r
450 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL\r
451 //\r
452 ASSERT(FilePath != NULL);\r
453 ASSERT(FileHandle != NULL);\r
454 ASSERT(DeviceHandle != NULL);\r
455 // \r
456 // which shell interface should we use\r
457 //\r
458 if (mEfiShellProtocol != NULL) {\r
459 //\r
460 // use UEFI Shell 2.0 method.\r
461 //\r
462 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
463 if (FileName == NULL) {\r
464 return (EFI_INVALID_PARAMETER);\r
465 }\r
466 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);\r
467 FreePool(FileName);\r
468 return (Status);\r
d2b4564b 469 } \r
470\r
471\r
472 //\r
473 // use old shell method.\r
474 //\r
475 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, \r
476 FilePath, \r
477 DeviceHandle);\r
478 if (EFI_ERROR (Status)) {\r
479 return Status;\r
480 }\r
481 Status = gBS->OpenProtocol(*DeviceHandle,\r
482 &gEfiSimpleFileSystemProtocolGuid,\r
b1f95a06 483 (VOID**)&EfiSimpleFileSystemProtocol,\r
d2b4564b 484 gImageHandle,\r
485 NULL,\r
486 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
487 if (EFI_ERROR (Status)) {\r
488 return Status;\r
489 }\r
490 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);\r
491 if (EFI_ERROR (Status)) {\r
492 FileHandle = NULL;\r
493 return Status;\r
494 }\r
495\r
496 //\r
497 // go down directories one node at a time.\r
498 //\r
499 while (!IsDevicePathEnd (*FilePath)) {\r
94b17fa1 500 //\r
d2b4564b 501 // For file system access each node should be a file path component\r
94b17fa1 502 //\r
d2b4564b 503 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
504 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
505 ) {\r
94b17fa1 506 FileHandle = NULL;\r
d2b4564b 507 return (EFI_INVALID_PARAMETER);\r
94b17fa1 508 }\r
d2b4564b 509 //\r
510 // Open this file path node\r
511 //\r
512 LastHandle = *FileHandle;\r
513 *FileHandle = NULL;\r
94b17fa1 514\r
515 //\r
d2b4564b 516 // Try to test opening an existing file\r
94b17fa1 517 //\r
d2b4564b 518 Status = LastHandle->Open (\r
519 LastHandle,\r
520 FileHandle,\r
521 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
522 OpenMode &~EFI_FILE_MODE_CREATE,\r
523 0\r
524 );\r
94b17fa1 525\r
d2b4564b 526 //\r
527 // see if the error was that it needs to be created\r
528 //\r
529 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
94b17fa1 530 Status = LastHandle->Open (\r
531 LastHandle,\r
532 FileHandle,\r
533 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
d2b4564b 534 OpenMode,\r
535 Attributes\r
94b17fa1 536 );\r
d2b4564b 537 }\r
538 //\r
539 // Close the last node\r
540 //\r
541 LastHandle->Close (LastHandle);\r
94b17fa1 542\r
d2b4564b 543 if (EFI_ERROR(Status)) {\r
544 return (Status);\r
94b17fa1 545 }\r
d2b4564b 546\r
547 //\r
548 // Get the next node\r
549 //\r
550 *FilePath = NextDevicePathNode (*FilePath);\r
94b17fa1 551 }\r
d2b4564b 552 return (EFI_SUCCESS);\r
94b17fa1 553}\r
554\r
555/**\r
556 This function will open a file or directory referenced by filename.\r
557\r
69817bf8 558 If return is EFI_SUCCESS, the Filehandle is the opened file's handle; \r
94b17fa1 559 otherwise, the Filehandle is NULL. The Attributes is valid only for \r
560 EFI_FILE_MODE_CREATE.\r
561\r
562 if FileNAme is NULL then ASSERT()\r
563\r
564 @param FileName pointer to file name\r
565 @param FileHandle pointer to the file handle.\r
566 @param OpenMode the mode to open the file with.\r
567 @param Attributes the file's file attributes.\r
568\r
569 @retval EFI_SUCCESS The information was set.\r
570 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
571 @retval EFI_UNSUPPORTED Could not open the file path. \r
572 @retval EFI_NOT_FOUND The specified file could not be found on the \r
573 device or the file system could not be found \r
574 on the device.\r
575 @retval EFI_NO_MEDIA The device has no medium.\r
576 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
577 medium is no longer supported.\r
578 @retval EFI_DEVICE_ERROR The device reported an error.\r
579 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
580 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
581 @retval EFI_ACCESS_DENIED The file was opened read only.\r
582 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
583 file.\r
584 @retval EFI_VOLUME_FULL The volume is full.\r
585**/\r
586EFI_STATUS\r
587EFIAPI\r
588ShellOpenFileByName(\r
b82bfcc1 589 IN CONST CHAR16 *FileName,\r
94b17fa1 590 OUT EFI_FILE_HANDLE *FileHandle,\r
591 IN UINT64 OpenMode,\r
592 IN UINT64 Attributes\r
2247dde4 593 ) {\r
94b17fa1 594 EFI_HANDLE DeviceHandle;\r
595 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
b1f95a06 596 EFI_STATUS Status;\r
597 EFI_FILE_INFO *FileInfo;\r
94b17fa1 598\r
599 //\r
600 // ASSERT if FileName is NULL\r
601 //\r
602 ASSERT(FileName != NULL);\r
603\r
604 if (mEfiShellProtocol != NULL) {\r
605 //\r
606 // Use UEFI Shell 2.0 method\r
607 //\r
b1f95a06 608 Status = mEfiShellProtocol->OpenFileByName(FileName,\r
609 FileHandle,\r
610 OpenMode);\r
2247dde4 611 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
612 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
b1f95a06 613 ASSERT(FileInfo != NULL);\r
614 FileInfo->Attribute = Attributes;\r
2247dde4 615 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r
616 FreePool(FileInfo);\r
b1f95a06 617 }\r
618 return (Status);\r
94b17fa1 619 } \r
620 //\r
621 // Using EFI Shell version\r
622 // this means convert name to path and call that function\r
623 // since this will use EFI method again that will open it.\r
624 //\r
625 ASSERT(mEfiShellEnvironment2 != NULL);\r
b82bfcc1 626 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
94b17fa1 627 if (FileDevicePath != NULL) {\r
628 return (ShellOpenFileByDevicePath(&FilePath,\r
629 &DeviceHandle,\r
630 FileHandle,\r
631 OpenMode,\r
632 Attributes ));\r
633 }\r
634 return (EFI_DEVICE_ERROR);\r
635}\r
636/**\r
637 This function create a directory\r
638\r
639 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle; \r
640 otherwise, the Filehandle is NULL. If the directory already existed, this \r
641 function opens the existing directory.\r
642\r
643 @param DirectoryName pointer to directory name\r
644 @param FileHandle pointer to the file handle.\r
645\r
646 @retval EFI_SUCCESS The information was set.\r
647 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
648 @retval EFI_UNSUPPORTED Could not open the file path. \r
649 @retval EFI_NOT_FOUND The specified file could not be found on the \r
650 device or the file system could not be found \r
651 on the device.\r
652 @retval EFI_NO_MEDIA The device has no medium.\r
653 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
654 medium is no longer supported.\r
655 @retval EFI_DEVICE_ERROR The device reported an error.\r
656 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
657 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
658 @retval EFI_ACCESS_DENIED The file was opened read only.\r
659 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
660 file.\r
661 @retval EFI_VOLUME_FULL The volume is full.\r
662 @sa ShellOpenFileByName\r
663**/\r
664EFI_STATUS\r
665EFIAPI\r
666ShellCreateDirectory(\r
b82bfcc1 667 IN CONST CHAR16 *DirectoryName,\r
94b17fa1 668 OUT EFI_FILE_HANDLE *FileHandle\r
2247dde4 669 ) {\r
670 if (mEfiShellProtocol != NULL) {\r
671 //\r
672 // Use UEFI Shell 2.0 method\r
673 //\r
674 return (mEfiShellProtocol->CreateFile(DirectoryName,\r
675 EFI_FILE_DIRECTORY,\r
676 FileHandle\r
677 ));\r
678 } else {\r
679 return (ShellOpenFileByName(DirectoryName,\r
680 FileHandle,\r
681 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r
682 EFI_FILE_DIRECTORY\r
683 ));\r
684 }\r
94b17fa1 685}\r
686\r
687/**\r
688 This function reads information from an opened file.\r
689\r
690 If FileHandle is not a directory, the function reads the requested number of \r
69817bf8 691 bytes from the file at the file's current position and returns them in Buffer. \r
94b17fa1 692 If the read goes beyond the end of the file, the read length is truncated to the\r
69817bf8 693 end of the file. The file's current position is increased by the number of bytes \r
94b17fa1 694 returned. If FileHandle is a directory, the function reads the directory entry \r
69817bf8 695 at the file's current position and returns the entry in Buffer. If the Buffer \r
94b17fa1 696 is not large enough to hold the current directory entry, then \r
697 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated. \r
698 BufferSize is set to be the size of the buffer needed to read the entry. On \r
699 success, the current position is updated to the next directory entry. If there \r
700 are no more directory entries, the read returns a zero-length buffer. \r
701 EFI_FILE_INFO is the structure returned as the directory entry.\r
702\r
703 @param FileHandle the opened file handle\r
704 @param BufferSize on input the size of buffer in bytes. on return \r
705 the number of bytes written.\r
706 @param Buffer the buffer to put read data into.\r
707\r
708 @retval EFI_SUCCESS Data was read.\r
709 @retval EFI_NO_MEDIA The device has no media.\r
710 @retval EFI_DEVICE_ERROR The device reported an error.\r
711 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
712 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required \r
713 size.\r
714\r
715**/\r
716EFI_STATUS\r
717EFIAPI\r
718ShellReadFile(\r
719 IN EFI_FILE_HANDLE FileHandle,\r
720 IN OUT UINTN *BufferSize,\r
721 OUT VOID *Buffer\r
2247dde4 722 ) {\r
d2b4564b 723 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 724}\r
725\r
726\r
727/**\r
728 Write data to a file.\r
729\r
730 This function writes the specified number of bytes to the file at the current \r
731 file position. The current file position is advanced the actual number of bytes \r
732 written, which is returned in BufferSize. Partial writes only occur when there \r
69817bf8 733 has been a data error during the write attempt (such as "volume space full"). \r
94b17fa1 734 The file is automatically grown to hold the data if required. Direct writes to \r
735 opened directories are not supported.\r
736\r
737 @param FileHandle The opened file for writing\r
738 @param BufferSize on input the number of bytes in Buffer. On output\r
739 the number of bytes written.\r
740 @param Buffer the buffer containing data to write is stored.\r
741\r
742 @retval EFI_SUCCESS Data was written.\r
743 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
744 @retval EFI_NO_MEDIA The device has no media.\r
745 @retval EFI_DEVICE_ERROR The device reported an error.\r
746 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
747 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
748 @retval EFI_ACCESS_DENIED The file was open for read only.\r
749 @retval EFI_VOLUME_FULL The volume is full.\r
750**/\r
751EFI_STATUS\r
752EFIAPI\r
753ShellWriteFile(\r
754 IN EFI_FILE_HANDLE FileHandle,\r
755 IN OUT UINTN *BufferSize,\r
756 IN VOID *Buffer\r
2247dde4 757 ) {\r
d2b4564b 758 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 759}\r
760\r
761/** \r
762 Close an open file handle.\r
763\r
69817bf8 764 This function closes a specified file handle. All "dirty" cached file data is \r
94b17fa1 765 flushed to the device, and the file is closed. In all cases the handle is \r
766 closed.\r
767\r
768@param FileHandle the file handle to close.\r
769\r
770@retval EFI_SUCCESS the file handle was closed sucessfully.\r
771**/\r
772EFI_STATUS\r
773EFIAPI\r
774ShellCloseFile (\r
775 IN EFI_FILE_HANDLE *FileHandle\r
2247dde4 776 ) {\r
d2b4564b 777 return (FileFunctionMap.CloseFile(*FileHandle));\r
94b17fa1 778}\r
779\r
780/**\r
781 Delete a file and close the handle\r
782\r
783 This function closes and deletes a file. In all cases the file handle is closed.\r
784 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is \r
785 returned, but the handle is still closed.\r
786\r
787 @param FileHandle the file handle to delete\r
788\r
789 @retval EFI_SUCCESS the file was closed sucessfully\r
790 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not \r
791 deleted\r
792 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
793**/\r
794EFI_STATUS\r
795EFIAPI\r
796ShellDeleteFile (\r
797 IN EFI_FILE_HANDLE *FileHandle\r
2247dde4 798 ) {\r
d2b4564b 799 return (FileFunctionMap.DeleteFile(*FileHandle));\r
94b17fa1 800}\r
801\r
802/**\r
803 Set the current position in a file.\r
804\r
805 This function sets the current file position for the handle to the position \r
806 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
807 absolute positioning is supported, and seeking past the end of the file is \r
808 allowed (a subsequent write would grow the file). Seeking to position \r
809 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
810 If FileHandle is a directory, the only position that may be set is zero. This \r
811 has the effect of starting the read process of the directory entries over.\r
812\r
813 @param FileHandle The file handle on which the position is being set\r
814 @param Position Byte position from begining of file\r
815\r
816 @retval EFI_SUCCESS Operation completed sucessfully.\r
817 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on \r
818 directories.\r
819 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
820**/\r
821EFI_STATUS\r
822EFIAPI\r
823ShellSetFilePosition (\r
824 IN EFI_FILE_HANDLE FileHandle,\r
825 IN UINT64 Position\r
2247dde4 826 ) {\r
d2b4564b 827 return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r
94b17fa1 828}\r
829\r
830/** \r
831 Gets a file's current position\r
832\r
833 This function retrieves the current file position for the file handle. For \r
834 directories, the current file position has no meaning outside of the file \r
835 system driver and as such the operation is not supported. An error is returned\r
836 if FileHandle is a directory.\r
837\r
838 @param FileHandle The open file handle on which to get the position.\r
839 @param Position Byte position from begining of file.\r
840\r
841 @retval EFI_SUCCESS the operation completed sucessfully.\r
842 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
843 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
844**/\r
845EFI_STATUS\r
846EFIAPI\r
847ShellGetFilePosition (\r
848 IN EFI_FILE_HANDLE FileHandle,\r
849 OUT UINT64 *Position\r
2247dde4 850 ) {\r
d2b4564b 851 return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r
94b17fa1 852}\r
853/**\r
854 Flushes data on a file\r
855 \r
856 This function flushes all modified data associated with a file to a device.\r
857\r
858 @param FileHandle The file handle on which to flush data\r
859\r
860 @retval EFI_SUCCESS The data was flushed.\r
861 @retval EFI_NO_MEDIA The device has no media.\r
862 @retval EFI_DEVICE_ERROR The device reported an error.\r
863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
864 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
865 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
866**/\r
867EFI_STATUS\r
868EFIAPI\r
869ShellFlushFile (\r
870 IN EFI_FILE_HANDLE FileHandle\r
2247dde4 871 ) {\r
d2b4564b 872 return (FileFunctionMap.FlushFile(FileHandle));\r
94b17fa1 873}\r
874\r
875/**\r
876 Retrieves the first file from a directory\r
877\r
69817bf8 878 This function opens a directory and gets the first file's info in the \r
94b17fa1 879 directory. Caller can use ShellFindNextFile() to get other files. When \r
880 complete the caller is responsible for calling FreePool() on Buffer.\r
881\r
882 @param DirHandle The file handle of the directory to search\r
883 @param Buffer Pointer to buffer for file's information\r
884\r
885 @retval EFI_SUCCESS Found the first file.\r
886 @retval EFI_NOT_FOUND Cannot find the directory.\r
887 @retval EFI_NO_MEDIA The device has no media.\r
888 @retval EFI_DEVICE_ERROR The device reported an error.\r
889 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
890 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
891 or ShellReadFile\r
892**/\r
893EFI_STATUS\r
894EFIAPI\r
895ShellFindFirstFile (\r
896 IN EFI_FILE_HANDLE DirHandle,\r
d2b4564b 897 OUT EFI_FILE_INFO **Buffer\r
2247dde4 898 ) {\r
94b17fa1 899 //\r
d2b4564b 900 // pass to file handle lib\r
94b17fa1 901 //\r
d2b4564b 902 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
94b17fa1 903}\r
904/**\r
905 Retrieves the next file in a directory.\r
906\r
907 To use this function, caller must call the LibFindFirstFile() to get the \r
908 first file, and then use this function get other files. This function can be \r
909 called for several times to get each file's information in the directory. If \r
910 the call of ShellFindNextFile() got the last file in the directory, the next \r
911 call of this function has no file to get. *NoFile will be set to TRUE and the \r
912 Buffer memory will be automatically freed. \r
913\r
914 @param DirHandle the file handle of the directory\r
915 @param Buffer pointer to buffer for file's information\r
916 @param NoFile pointer to boolean when last file is found\r
917\r
918 @retval EFI_SUCCESS Found the next file, or reached last file\r
919 @retval EFI_NO_MEDIA The device has no media.\r
920 @retval EFI_DEVICE_ERROR The device reported an error.\r
921 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
922**/\r
923EFI_STATUS\r
924EFIAPI\r
925ShellFindNextFile(\r
926 IN EFI_FILE_HANDLE DirHandle,\r
927 OUT EFI_FILE_INFO *Buffer,\r
928 OUT BOOLEAN *NoFile\r
2247dde4 929 ) {\r
94b17fa1 930 //\r
d2b4564b 931 // pass to file handle lib\r
94b17fa1 932 //\r
d2b4564b 933 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
94b17fa1 934}\r
935/**\r
936 Retrieve the size of a file.\r
937\r
938 if FileHandle is NULL then ASSERT()\r
939 if Size is NULL then ASSERT()\r
940\r
69817bf8 941 This function extracts the file size info from the FileHandle's EFI_FILE_INFO \r
94b17fa1 942 data.\r
943\r
944 @param FileHandle file handle from which size is retrieved\r
945 @param Size pointer to size\r
946\r
947 @retval EFI_SUCCESS operation was completed sucessfully\r
948 @retval EFI_DEVICE_ERROR cannot access the file\r
949**/\r
950EFI_STATUS\r
951EFIAPI\r
952ShellGetFileSize (\r
953 IN EFI_FILE_HANDLE FileHandle,\r
954 OUT UINT64 *Size\r
2247dde4 955 ) {\r
d2b4564b 956 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
94b17fa1 957}\r
958/**\r
959 Retrieves the status of the break execution flag\r
960\r
961 this function is useful to check whether the application is being asked to halt by the shell.\r
962\r
963 @retval TRUE the execution break is enabled\r
964 @retval FALSE the execution break is not enabled\r
965**/\r
966BOOLEAN\r
967EFIAPI\r
968ShellGetExecutionBreakFlag(\r
969 VOID\r
970 )\r
971{\r
972 // \r
973 // Check for UEFI Shell 2.0 protocols\r
974 //\r
975 if (mEfiShellProtocol != NULL) {\r
976\r
977 //\r
978 // We are using UEFI Shell 2.0; see if the event has been triggered\r
979 //\r
980 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
981 return (FALSE);\r
982 }\r
983 return (TRUE);\r
984 } \r
985\r
986 //\r
987 // using EFI Shell; call the function to check\r
988 //\r
989 ASSERT(mEfiShellEnvironment2 != NULL);\r
990 return (mEfiShellEnvironment2->GetExecutionBreak());\r
991}\r
992/**\r
993 return the value of an environment variable\r
994\r
995 this function gets the value of the environment variable set by the \r
996 ShellSetEnvironmentVariable function\r
997\r
998 @param EnvKey The key name of the environment variable.\r
999\r
1000 @retval NULL the named environment variable does not exist.\r
1001 @return != NULL pointer to the value of the environment variable\r
1002**/\r
1003CONST CHAR16*\r
1004EFIAPI\r
1005ShellGetEnvironmentVariable (\r
9b3bf083 1006 IN CONST CHAR16 *EnvKey\r
94b17fa1 1007 )\r
1008{\r
1009 // \r
1010 // Check for UEFI Shell 2.0 protocols\r
1011 //\r
1012 if (mEfiShellProtocol != NULL) {\r
1013 return (mEfiShellProtocol->GetEnv(EnvKey));\r
1014 }\r
1015\r
1016 //\r
1017 // ASSERT that we must have EFI shell\r
1018 //\r
1019 ASSERT(mEfiShellEnvironment2 != NULL);\r
1020\r
1021 //\r
1022 // using EFI Shell\r
1023 //\r
9b3bf083 1024 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
94b17fa1 1025}\r
1026/**\r
1027 set the value of an environment variable\r
1028\r
1029This function changes the current value of the specified environment variable. If the\r
1030environment variable exists and the Value is an empty string, then the environment\r
1031variable is deleted. If the environment variable exists and the Value is not an empty\r
1032string, then the value of the environment variable is changed. If the environment\r
1033variable does not exist and the Value is an empty string, there is no action. If the\r
1034environment variable does not exist and the Value is a non-empty string, then the\r
1035environment variable is created and assigned the specified value.\r
1036\r
1037 This is not supported pre-UEFI Shell 2.0.\r
1038\r
1039 @param EnvKey The key name of the environment variable.\r
1040 @param EnvVal The Value of the environment variable\r
1041 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1042\r
1043 @retval EFI_SUCCESS the operation was completed sucessfully\r
1044 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1045**/\r
1046EFI_STATUS\r
1047EFIAPI\r
1048ShellSetEnvironmentVariable (\r
1049 IN CONST CHAR16 *EnvKey,\r
1050 IN CONST CHAR16 *EnvVal,\r
1051 IN BOOLEAN Volatile\r
1052 )\r
1053{\r
1054 // \r
1055 // Check for UEFI Shell 2.0 protocols\r
1056 //\r
1057 if (mEfiShellProtocol != NULL) {\r
1058 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
1059 } \r
1060\r
1061 //\r
1062 // This feature does not exist under EFI shell\r
1063 //\r
1064 return (EFI_UNSUPPORTED);\r
1065}\r
1066/**\r
1067 cause the shell to parse and execute a command line.\r
1068\r
1069 This function creates a nested instance of the shell and executes the specified\r
1070command (CommandLine) with the specified environment (Environment). Upon return,\r
1071the status code returned by the specified command is placed in StatusCode.\r
1072If Environment is NULL, then the current environment is used and all changes made\r
1073by the commands executed will be reflected in the current environment. If the\r
1074Environment is non-NULL, then the changes made will be discarded.\r
1075The CommandLine is executed from the current working directory on the current\r
1076device.\r
1077\r
1078EnvironmentVariables and Status are only supported for UEFI Shell 2.0.\r
1079Output is only supported for pre-UEFI Shell 2.0\r
1080\r
1081 @param ImageHandle Parent image that is starting the operation\r
1082 @param CommandLine pointer to null terminated command line.\r
1083 @param Output true to display debug output. false to hide it.\r
1084 @param EnvironmentVariables optional pointer to array of environment variables\r
1085 in the form "x=y". if NULL current set is used.\r
1086 @param Status the status of the run command line.\r
1087\r
1088 @retval EFI_SUCCESS the operation completed sucessfully. Status\r
1089 contains the status code returned.\r
1090 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value\r
1091 @retval EFI_OUT_OF_RESOURCES out of resources\r
1092 @retval EFI_UNSUPPORTED the operation is not allowed.\r
1093**/\r
1094EFI_STATUS\r
1095EFIAPI\r
1096ShellExecute (\r
1097 IN EFI_HANDLE *ParentHandle,\r
1098 IN CHAR16 *CommandLine OPTIONAL,\r
1099 IN BOOLEAN Output OPTIONAL,\r
1100 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1101 OUT EFI_STATUS *Status OPTIONAL\r
1102 )\r
1103{\r
1104 // \r
1105 // Check for UEFI Shell 2.0 protocols\r
1106 //\r
1107 if (mEfiShellProtocol != NULL) {\r
1108 //\r
1109 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1110 //\r
1111 return (mEfiShellProtocol->Execute(ParentHandle,\r
1112 CommandLine,\r
1113 EnvironmentVariables,\r
1114 Status));\r
1115 } \r
1116 //\r
1117 // ASSERT that we must have EFI shell\r
1118 //\r
1119 ASSERT(mEfiShellEnvironment2 != NULL);\r
1120 //\r
1121 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
1122 // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
1123 //\r
1124 return (mEfiShellEnvironment2->Execute(*ParentHandle, \r
1125 CommandLine, \r
1126 Output));\r
1127}\r
1128/**\r
1129 Retreives the current directory path\r
1130\r
69817bf8 1131 If the DeviceName is NULL, it returns the current device's current directory \r
94b17fa1 1132 name. If the DeviceName is not NULL, it returns the current directory name \r
1133 on specified drive.\r
1134\r
1135 @param DeviceName the name of the drive to get directory on\r
1136\r
1137 @retval NULL the directory does not exist\r
1138 @return != NULL the directory\r
1139**/\r
1140CONST CHAR16*\r
1141EFIAPI\r
1142ShellGetCurrentDir (\r
1143 IN CHAR16 *DeviceName OPTIONAL\r
1144 )\r
1145{\r
1146 // \r
1147 // Check for UEFI Shell 2.0 protocols\r
1148 //\r
1149 if (mEfiShellProtocol != NULL) {\r
1150 return (mEfiShellProtocol->GetCurDir(DeviceName));\r
1151 } \r
1152 //\r
1153 // ASSERT that we must have EFI shell\r
1154 //\r
1155 ASSERT(mEfiShellEnvironment2 != NULL);\r
1156 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1157}\r
1158/**\r
1159 sets (enabled or disabled) the page break mode\r
1160\r
1161 when page break mode is enabled the screen will stop scrolling \r
1162 and wait for operator input before scrolling a subsequent screen.\r
1163\r
1164 @param CurrentState TRUE to enable and FALSE to disable\r
1165**/\r
1166VOID \r
1167EFIAPI\r
1168ShellSetPageBreakMode (\r
1169 IN BOOLEAN CurrentState\r
1170 )\r
1171{\r
1172 //\r
1173 // check for enabling\r
1174 //\r
1175 if (CurrentState != 0x00) {\r
1176 // \r
1177 // check for UEFI Shell 2.0\r
1178 //\r
1179 if (mEfiShellProtocol != NULL) {\r
1180 //\r
1181 // Enable with UEFI 2.0 Shell\r
1182 //\r
1183 mEfiShellProtocol->EnablePageBreak();\r
1184 return;\r
1185 } else {\r
1186 // \r
1187 // ASSERT that must have EFI Shell\r
1188 //\r
1189 ASSERT(mEfiShellEnvironment2 != NULL);\r
1190 //\r
1191 // Enable with EFI Shell\r
1192 //\r
1193 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1194 return;\r
1195 }\r
1196 } else {\r
1197 // \r
1198 // check for UEFI Shell 2.0\r
1199 //\r
1200 if (mEfiShellProtocol != NULL) {\r
1201 //\r
1202 // Disable with UEFI 2.0 Shell\r
1203 //\r
1204 mEfiShellProtocol->DisablePageBreak();\r
1205 return;\r
1206 } else {\r
1207 // \r
1208 // ASSERT that must have EFI Shell\r
1209 //\r
1210 ASSERT(mEfiShellEnvironment2 != NULL);\r
1211 //\r
1212 // Disable with EFI Shell\r
1213 //\r
1214 mEfiShellEnvironment2->DisablePageBreak ();\r
1215 return;\r
1216 }\r
1217 }\r
1218}\r
1219\r
1220///\r
1221/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1222/// This allows for the struct to be populated.\r
1223///\r
1224typedef struct {\r
d2b4564b 1225 LIST_ENTRY Link;\r
94b17fa1 1226 EFI_STATUS Status;\r
1227 CHAR16 *FullName;\r
1228 CHAR16 *FileName;\r
1229 EFI_FILE_HANDLE Handle;\r
1230 EFI_FILE_INFO *Info;\r
1231} EFI_SHELL_FILE_INFO_NO_CONST;\r
1232\r
1233/**\r
1234 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1235\r
1236 if OldStyleFileList is NULL then ASSERT()\r
1237\r
1238 this function will convert a SHELL_FILE_ARG based list into a callee allocated \r
1239 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1240 the ShellCloseFileMetaArg function.\r
1241\r
9b3bf083 1242 @param[in] FileList the EFI shell list type\r
b82bfcc1 1243 @param[in,out] ListHead the list to add to\r
94b17fa1 1244\r
1245 @retval the resultant head of the double linked new format list;\r
1246**/\r
1247LIST_ENTRY*\r
1248EFIAPI\r
1249InternalShellConvertFileListType (\r
9b3bf083 1250 IN LIST_ENTRY *FileList,\r
1251 IN OUT LIST_ENTRY *ListHead\r
125c2cf4 1252 )\r
1253{\r
94b17fa1 1254 SHELL_FILE_ARG *OldInfo;\r
9b3bf083 1255 LIST_ENTRY *Link;\r
94b17fa1 1256 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1257\r
1258 //\r
9b3bf083 1259 // ASSERTs\r
94b17fa1 1260 //\r
9b3bf083 1261 ASSERT(FileList != NULL);\r
1262 ASSERT(ListHead != NULL);\r
94b17fa1 1263\r
1264 //\r
1265 // enumerate through each member of the old list and copy\r
1266 //\r
d2b4564b 1267 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
94b17fa1 1268 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
1269\r
1270 //\r
1271 // make sure the old list was valid\r
1272 //\r
1273 ASSERT(OldInfo != NULL); \r
1274 ASSERT(OldInfo->Info != NULL);\r
1275 ASSERT(OldInfo->FullName != NULL);\r
1276 ASSERT(OldInfo->FileName != NULL);\r
1277\r
1278 //\r
1279 // allocate a new EFI_SHELL_FILE_INFO object\r
1280 //\r
1281 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1282 \r
1283 // \r
1284 // copy the simple items\r
1285 //\r
1286 NewInfo->Handle = OldInfo->Handle;\r
1287 NewInfo->Status = OldInfo->Status;\r
1288\r
d2b4564b 1289 // old shell checks for 0 not NULL\r
1290 OldInfo->Handle = 0;\r
1291\r
94b17fa1 1292 //\r
1293 // allocate new space to copy strings and structure\r
1294 //\r
1295 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r
1296 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r
1297 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r
1298 \r
1299 //\r
1300 // make sure all the memory allocations were sucessful\r
1301 //\r
1302 ASSERT(NewInfo->FullName != NULL);\r
1303 ASSERT(NewInfo->FileName != NULL);\r
1304 ASSERT(NewInfo->Info != NULL);\r
1305\r
1306 //\r
1307 // Copt the strings and structure\r
1308 //\r
1309 StrCpy(NewInfo->FullName, OldInfo->FullName);\r
1310 StrCpy(NewInfo->FileName, OldInfo->FileName);\r
1311 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r
1312\r
1313 //\r
1314 // add that to the list\r
1315 //\r
9b3bf083 1316 InsertTailList(ListHead, &NewInfo->Link);\r
94b17fa1 1317 }\r
1318 return (ListHead);\r
1319}\r
1320/**\r
1321 Opens a group of files based on a path.\r
1322\r
1323 This function uses the Arg to open all the matching files. Each matched \r
1324 file has a SHELL_FILE_ARG structure to record the file information. These \r
1325 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG \r
1326 structures from ListHead to access each file. This function supports wildcards\r
1327 and will process '?' and '*' as such. the list must be freed with a call to \r
1328 ShellCloseFileMetaArg().\r
1329\r
5f7431d0 1330 If you are NOT appending to an existing list *ListHead must be NULL. If \r
1331 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1332\r
1333 @param Arg pointer to path string\r
1334 @param OpenMode mode to open files with\r
1335 @param ListHead head of linked list of results\r
1336\r
1337 @retval EFI_SUCCESS the operation was sucessful and the list head \r
1338 contains the list of opened files\r
1339 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.\r
1340 *ListHead is set to NULL.\r
1341 @return != EFI_SUCCESS the operation failed\r
1342\r
1343 @sa InternalShellConvertFileListType\r
1344**/\r
1345EFI_STATUS\r
1346EFIAPI\r
1347ShellOpenFileMetaArg (\r
1348 IN CHAR16 *Arg,\r
1349 IN UINT64 OpenMode,\r
1350 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1351 )\r
1352{\r
1353 EFI_STATUS Status;\r
9b3bf083 1354 LIST_ENTRY mOldStyleFileList;\r
d2b4564b 1355 \r
94b17fa1 1356 //\r
1357 // ASSERT that Arg and ListHead are not NULL\r
1358 //\r
1359 ASSERT(Arg != NULL);\r
1360 ASSERT(ListHead != NULL);\r
1361\r
1362 // \r
1363 // Check for UEFI Shell 2.0 protocols\r
1364 //\r
1365 if (mEfiShellProtocol != NULL) {\r
5f7431d0 1366 if (*ListHead == NULL) {\r
1367 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1368 if (*ListHead == NULL) {\r
1369 return (EFI_OUT_OF_RESOURCES);\r
1370 }\r
1371 InitializeListHead(&((*ListHead)->Link));\r
1372 } \r
2247dde4 1373 Status = mEfiShellProtocol->OpenFileList(Arg, \r
94b17fa1 1374 OpenMode, \r
2247dde4 1375 ListHead);\r
1376 if (EFI_ERROR(Status)) {\r
1377 mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1378 } else {\r
1379 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1380 }\r
1381 return (Status);\r
94b17fa1 1382 } \r
1383\r
1384 //\r
1385 // ASSERT that we must have EFI shell\r
1386 //\r
1387 ASSERT(mEfiShellEnvironment2 != NULL);\r
1388\r
94b17fa1 1389 //\r
1390 // make sure the list head is initialized\r
1391 //\r
9b3bf083 1392 InitializeListHead(&mOldStyleFileList);\r
94b17fa1 1393\r
1394 //\r
1395 // Get the EFI Shell list of files\r
1396 //\r
9b3bf083 1397 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
94b17fa1 1398 if (EFI_ERROR(Status)) {\r
1399 *ListHead = NULL;\r
1400 return (Status);\r
1401 }\r
1402\r
9b3bf083 1403 if (*ListHead == NULL) {\r
1404 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1405 if (*ListHead == NULL) {\r
1406 return (EFI_OUT_OF_RESOURCES);\r
1407 }\r
1408 }\r
1409\r
94b17fa1 1410 //\r
1411 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1412 //\r
9b3bf083 1413 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1414\r
d2b4564b 1415 //\r
1416 // Free the EFI Shell version that was converted.\r
1417 //\r
9b3bf083 1418 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
94b17fa1 1419\r
1420 return (Status);\r
1421}\r
1422/**\r
1423 Free the linked list returned from ShellOpenFileMetaArg\r
1424\r
1425 if ListHead is NULL then ASSERT()\r
1426\r
1427 @param ListHead the pointer to free\r
1428\r
1429 @retval EFI_SUCCESS the operation was sucessful\r
1430**/\r
1431EFI_STATUS\r
1432EFIAPI\r
1433ShellCloseFileMetaArg (\r
1434 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1435 )\r
1436{\r
1437 LIST_ENTRY *Node;\r
1438\r
1439 //\r
1440 // ASSERT that ListHead is not NULL\r
1441 //\r
1442 ASSERT(ListHead != NULL);\r
1443\r
1444 // \r
1445 // Check for UEFI Shell 2.0 protocols\r
1446 //\r
1447 if (mEfiShellProtocol != NULL) {\r
1448 return (mEfiShellProtocol->FreeFileList(ListHead));\r
1449 } else {\r
94b17fa1 1450 //\r
1451 // Since this is EFI Shell version we need to free our internally made copy \r
1452 // of the list\r
1453 //\r
9b3bf083 1454 for ( Node = GetFirstNode(&(*ListHead)->Link) \r
1455 ; IsListEmpty(&(*ListHead)->Link) == FALSE \r
1456 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
94b17fa1 1457 RemoveEntryList(Node);\r
d2b4564b 1458 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
94b17fa1 1459 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1460 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1462 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1463 }\r
1464 return EFI_SUCCESS;\r
1465 }\r
1466}\r
1467\r
125c2cf4 1468/**\r
1469 Find a file by searching the CWD and then the path.\r
1470\r
b3011f40 1471 If FileName is NULL then ASSERT.\r
125c2cf4 1472\r
b3011f40 1473 If the return value is not NULL then the memory must be caller freed.\r
125c2cf4 1474\r
1475 @param FileName Filename string.\r
1476\r
1477 @retval NULL the file was not found\r
1478 @return !NULL the full path to the file.\r
1479**/\r
1480CHAR16 *\r
1481EFIAPI\r
1482ShellFindFilePath (\r
1483 IN CONST CHAR16 *FileName\r
1484 )\r
1485{\r
1486 CONST CHAR16 *Path;\r
1487 EFI_FILE_HANDLE Handle;\r
1488 EFI_STATUS Status;\r
1489 CHAR16 *RetVal;\r
1490 CHAR16 *TestPath;\r
1491 CONST CHAR16 *Walker;\r
36a9d672 1492 UINTN Size;\r
125c2cf4 1493\r
1494 RetVal = NULL;\r
1495\r
1496 Path = ShellGetEnvironmentVariable(L"cwd");\r
1497 if (Path != NULL) {\r
36a9d672 1498 Size = StrSize(Path);\r
1499 Size += StrSize(FileName);\r
1500 TestPath = AllocateZeroPool(Size);\r
125c2cf4 1501 StrCpy(TestPath, Path);\r
1502 StrCat(TestPath, FileName);\r
1503 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1504 if (!EFI_ERROR(Status)){\r
1505 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1506 ShellCloseFile(&Handle);\r
1507 FreePool(TestPath);\r
1508 return (RetVal);\r
1509 }\r
1510 FreePool(TestPath);\r
1511 }\r
1512 Path = ShellGetEnvironmentVariable(L"path");\r
1513 if (Path != NULL) {\r
36a9d672 1514 Size = StrSize(Path);\r
1515 Size += StrSize(FileName);\r
1516 TestPath = AllocateZeroPool(Size);\r
125c2cf4 1517 Walker = (CHAR16*)Path; \r
1518 do {\r
1519 CopyMem(TestPath, Walker, StrSize(Walker));\r
1520 if (StrStr(TestPath, L";") != NULL) {\r
1521 *(StrStr(TestPath, L";")) = CHAR_NULL;\r
1522 }\r
1523 StrCat(TestPath, FileName);\r
1524 if (StrStr(Walker, L";") != NULL) {\r
1525 Walker = StrStr(Walker, L";") + 1;\r
1526 } else {\r
1527 Walker = NULL;\r
1528 }\r
1529 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1530 if (!EFI_ERROR(Status)){\r
1531 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1532 ShellCloseFile(&Handle);\r
1533 break;\r
1534 }\r
1535 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1536 FreePool(TestPath);\r
1537 }\r
1538 return (RetVal);\r
1539}\r
1540\r
b3011f40 1541/**\r
1542 Find a file by searching the CWD and then the path with a variable set of file \r
1543 extensions. If the file is not found it will append each extension in the list \r
1544 in the order provided and return the first one that is successful.\r
1545\r
1546 If FileName is NULL, then ASSERT.\r
1547 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1548\r
1549 If the return value is not NULL then the memory must be caller freed.\r
1550\r
1551 @param[in] FileName Filename string.\r
1552 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1553\r
1554 @retval NULL The file was not found.\r
1555 @retval !NULL The path to the file.\r
1556**/\r
1557CHAR16 *\r
1558EFIAPI\r
1559ShellFindFilePathEx (\r
1560 IN CONST CHAR16 *FileName,\r
1561 IN CONST CHAR16 *FileExtension\r
1562 )\r
1563{\r
1564 CHAR16 *TestPath;\r
1565 CHAR16 *RetVal;\r
1566 CONST CHAR16 *ExtensionWalker;\r
1567 ASSERT(FileName != NULL);\r
1568 if (FileExtension == NULL) {\r
1569 return (ShellFindFilePath(FileName));\r
1570 }\r
1571 RetVal = ShellFindFilePath(FileName);\r
1572 if (RetVal != NULL) {\r
1573 return (RetVal);\r
1574 }\r
1575 TestPath = AllocateZeroPool(StrSize(FileName) + StrSize(FileExtension));\r
1576 for (ExtensionWalker = FileExtension ; ; ExtensionWalker = StrStr(ExtensionWalker, L";") + 1 ){\r
1577 StrCpy(TestPath, FileName);\r
1578 StrCat(TestPath, ExtensionWalker);\r
1579 if (StrStr(TestPath, L";") != NULL) {\r
1580 *(StrStr(TestPath, L";")) = CHAR_NULL;\r
1581 }\r
1582 RetVal = ShellFindFilePath(TestPath);\r
1583 if (RetVal != NULL) {\r
1584 break;\r
1585 }\r
1586 //\r
1587 // Must be after first loop...\r
1588 //\r
1589 if (StrStr(ExtensionWalker, L";") == NULL) {\r
1590 break;\r
1591 }\r
1592 }\r
1593 FreePool(TestPath);\r
1594 return (RetVal);\r
1595}\r
1596\r
94b17fa1 1597typedef struct {\r
9b3bf083 1598 LIST_ENTRY Link;\r
94b17fa1 1599 CHAR16 *Name;\r
1600 ParamType Type;\r
1601 CHAR16 *Value;\r
1602 UINTN OriginalPosition;\r
1603} SHELL_PARAM_PACKAGE;\r
1604\r
1605/**\r
1606 Checks the list of valid arguments and returns TRUE if the item was found. If the \r
1607 return value is TRUE then the type parameter is set also.\r
1608 \r
1609 if CheckList is NULL then ASSERT();\r
1610 if Name is NULL then ASSERT();\r
1611 if Type is NULL then ASSERT();\r
1612\r
1613 @param Type pointer to type of parameter if it was found\r
1614 @param Name pointer to Name of parameter found\r
1615 @param CheckList List to check against\r
1616\r
1617 @retval TRUE the Parameter was found. Type is valid.\r
1618 @retval FALSE the Parameter was not found. Type is not valid.\r
1619**/\r
1620BOOLEAN\r
1621EFIAPI\r
d2b4564b 1622InternalIsOnCheckList (\r
94b17fa1 1623 IN CONST CHAR16 *Name,\r
1624 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1625 OUT ParamType *Type\r
2247dde4 1626 ) {\r
94b17fa1 1627 SHELL_PARAM_ITEM *TempListItem;\r
1628\r
1629 //\r
1630 // ASSERT that all 3 pointer parameters aren't NULL\r
1631 //\r
1632 ASSERT(CheckList != NULL);\r
1633 ASSERT(Type != NULL);\r
1634 ASSERT(Name != NULL);\r
1635\r
d2b4564b 1636 //\r
1637 // question mark and page break mode are always supported\r
1638 //\r
1639 if ((StrCmp(Name, L"-?") == 0) ||\r
1640 (StrCmp(Name, L"-b") == 0)\r
1641 ) {\r
1642 return (TRUE);\r
1643 }\r
1644\r
94b17fa1 1645 //\r
1646 // Enumerate through the list\r
1647 //\r
1648 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1649 //\r
9eb53ac3 1650 // If the Type is TypeStart only check the first characters of the passed in param\r
1651 // If it matches set the type and return TRUE\r
94b17fa1 1652 //\r
9eb53ac3 1653 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1654 *Type = TempListItem->Type;\r
1655 return (TRUE);\r
1656 } else if (StrCmp(Name, TempListItem->Name) == 0) {\r
94b17fa1 1657 *Type = TempListItem->Type;\r
1658 return (TRUE);\r
1659 }\r
1660 }\r
2247dde4 1661\r
94b17fa1 1662 return (FALSE);\r
1663}\r
1664/**\r
d2b4564b 1665 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 1666\r
1667 @param Name pointer to Name of parameter found\r
1668\r
1669 @retval TRUE the Parameter is a flag.\r
1670 @retval FALSE the Parameter not a flag\r
1671**/\r
1672BOOLEAN\r
1673EFIAPI\r
d2b4564b 1674InternalIsFlag (\r
2247dde4 1675 IN CONST CHAR16 *Name,\r
1676 IN BOOLEAN AlwaysAllowNumbers\r
94b17fa1 1677 )\r
1678{\r
1679 //\r
1680 // ASSERT that Name isn't NULL\r
1681 //\r
1682 ASSERT(Name != NULL);\r
1683\r
2247dde4 1684 //\r
1685 // If we accept numbers then dont return TRUE. (they will be values)\r
1686 //\r
b3011f40 1687 if (((Name[0] == L'-' || Name[0] == L'+') && ShellLibIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {\r
2247dde4 1688 return (FALSE);\r
1689 }\r
1690\r
94b17fa1 1691 //\r
1692 // If the Name has a / or - as the first character return TRUE\r
1693 //\r
d2b4564b 1694 if ((Name[0] == L'/') || \r
1695 (Name[0] == L'-') ||\r
1696 (Name[0] == L'+')\r
1697 ) {\r
94b17fa1 1698 return (TRUE);\r
1699 }\r
1700 return (FALSE);\r
1701}\r
1702\r
1703/**\r
1704 Checks the command line arguments passed against the list of valid ones. \r
1705\r
1706 If no initialization is required, then return RETURN_SUCCESS.\r
1707 \r
1708 @param CheckList pointer to list of parameters to check\r
1709 @param CheckPackage pointer to pointer to list checked values\r
1710 @param ProblemParam optional pointer to pointer to unicode string for \r
d2b4564b 1711 the paramater that caused failure. If used then the\r
1712 caller is responsible for freeing the memory.\r
94b17fa1 1713 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1714 @param Argc Count of parameters in Argv\r
1715 @param Argv pointer to array of parameters\r
1716\r
1717 @retval EFI_SUCCESS The operation completed sucessfully.\r
1718 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1719 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1720 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1721 duplicated. the duplicated command line argument \r
1722 was returned in ProblemParam if provided.\r
1723 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1724 the invalid command line argument was returned in\r
1725 ProblemParam if provided.\r
1726**/\r
2247dde4 1727STATIC\r
94b17fa1 1728EFI_STATUS\r
1729EFIAPI\r
1730InternalCommandLineParse (\r
1731 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1732 OUT LIST_ENTRY **CheckPackage,\r
1733 OUT CHAR16 **ProblemParam OPTIONAL,\r
1734 IN BOOLEAN AutoPageBreak,\r
1735 IN CONST CHAR16 **Argv,\r
2247dde4 1736 IN UINTN Argc,\r
1737 IN BOOLEAN AlwaysAllowNumbers\r
1738 ) {\r
94b17fa1 1739 UINTN LoopCounter;\r
94b17fa1 1740 ParamType CurrentItemType;\r
1741 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
125c2cf4 1742 UINTN GetItemValue;\r
1743 UINTN ValueSize;\r
94b17fa1 1744\r
1745 CurrentItemPackage = NULL;\r
2247dde4 1746 mTotalParameterCount = 0;\r
125c2cf4 1747 GetItemValue = 0;\r
1748 ValueSize = 0;\r
94b17fa1 1749\r
1750 //\r
1751 // If there is only 1 item we dont need to do anything\r
1752 //\r
1753 if (Argc <= 1) {\r
1754 *CheckPackage = NULL;\r
1755 return (EFI_SUCCESS);\r
1756 }\r
1757\r
2247dde4 1758 //\r
1759 // ASSERTs\r
1760 //\r
1761 ASSERT(CheckList != NULL);\r
1762 ASSERT(Argv != NULL);\r
1763\r
94b17fa1 1764 //\r
1765 // initialize the linked list\r
1766 //\r
1767 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1768 InitializeListHead(*CheckPackage);\r
1769\r
1770 //\r
1771 // loop through each of the arguments\r
1772 //\r
1773 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1774 if (Argv[LoopCounter] == NULL) {\r
1775 //\r
1776 // do nothing for NULL argv\r
1777 //\r
b3011f40 1778 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {\r
2247dde4 1779 //\r
1780 // We might have leftover if last parameter didnt have optional value\r
1781 //\r
125c2cf4 1782 if (GetItemValue != 0) {\r
1783 GetItemValue = 0;\r
2247dde4 1784 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1785 }\r
94b17fa1 1786 //\r
1787 // this is a flag\r
1788 //\r
1789 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1790 ASSERT(CurrentItemPackage != NULL);\r
1791 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r
1792 ASSERT(CurrentItemPackage->Name != NULL);\r
1793 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1794 CurrentItemPackage->Type = CurrentItemType;\r
1795 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 1796 CurrentItemPackage->Value = NULL;\r
94b17fa1 1797\r
1798 //\r
1799 // Does this flag require a value\r
1800 //\r
125c2cf4 1801 switch (CurrentItemPackage->Type) {\r
94b17fa1 1802 //\r
125c2cf4 1803 // possibly trigger the next loop(s) to populate the value of this item\r
1804 // \r
1805 case TypeValue:\r
1806 GetItemValue = 1; \r
1807 ValueSize = 0;\r
1808 break;\r
1809 case TypeDoubleValue:\r
1810 GetItemValue = 2;\r
1811 ValueSize = 0;\r
1812 break;\r
1813 case TypeMaxValue:\r
1814 GetItemValue = (UINTN)(-1);\r
1815 ValueSize = 0;\r
1816 break;\r
1817 default:\r
1818 //\r
1819 // this item has no value expected; we are done\r
1820 //\r
1821 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1822 ASSERT(GetItemValue == 0);\r
1823 break;\r
94b17fa1 1824 }\r
125c2cf4 1825 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
b1f95a06 1826 ASSERT(CurrentItemPackage != NULL);\r
1827 //\r
125c2cf4 1828 // get the item VALUE for a previous flag\r
b1f95a06 1829 //\r
125c2cf4 1830 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
b1f95a06 1831 ASSERT(CurrentItemPackage->Value != NULL);\r
125c2cf4 1832 if (ValueSize == 0) {\r
1833 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1834 } else {\r
1835 StrCat(CurrentItemPackage->Value, L" ");\r
1836 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1837 }\r
1838 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
1839 GetItemValue--;\r
1840 if (GetItemValue == 0) {\r
1841 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1842 }\r
2247dde4 1843 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
b1f95a06 1844 //\r
1845 // add this one as a non-flag\r
1846 //\r
1847 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1848 ASSERT(CurrentItemPackage != NULL);\r
1849 CurrentItemPackage->Name = NULL;\r
1850 CurrentItemPackage->Type = TypePosition;\r
1851 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r
1852 ASSERT(CurrentItemPackage->Value != NULL);\r
1853 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2247dde4 1854 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;\r
9b3bf083 1855 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
94b17fa1 1856 } else if (ProblemParam) {\r
1857 //\r
1858 // this was a non-recognised flag... error!\r
1859 //\r
d2b4564b 1860 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r
1861 ASSERT(*ProblemParam != NULL);\r
1862 StrCpy(*ProblemParam, Argv[LoopCounter]);\r
94b17fa1 1863 ShellCommandLineFreeVarList(*CheckPackage);\r
1864 *CheckPackage = NULL;\r
1865 return (EFI_VOLUME_CORRUPTED);\r
1866 } else {\r
1867 ShellCommandLineFreeVarList(*CheckPackage);\r
1868 *CheckPackage = NULL;\r
1869 return (EFI_VOLUME_CORRUPTED);\r
1870 }\r
1871 }\r
125c2cf4 1872 if (GetItemValue != 0) {\r
1873 GetItemValue = 0;\r
1874 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1875 }\r
94b17fa1 1876 //\r
1877 // support for AutoPageBreak\r
1878 //\r
1879 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
1880 ShellSetPageBreakMode(TRUE);\r
1881 }\r
1882 return (EFI_SUCCESS);\r
1883}\r
1884\r
1885/**\r
1886 Checks the command line arguments passed against the list of valid ones. \r
1887 Optionally removes NULL values first.\r
1888 \r
1889 If no initialization is required, then return RETURN_SUCCESS.\r
1890 \r
1891 @param CheckList pointer to list of parameters to check\r
1892 @param CheckPackage pointer to pointer to list checked values\r
1893 @param ProblemParam optional pointer to pointer to unicode string for \r
1894 the paramater that caused failure.\r
1895 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1896\r
1897 @retval EFI_SUCCESS The operation completed sucessfully.\r
1898 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1899 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1900 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1901 duplicated. the duplicated command line argument \r
1902 was returned in ProblemParam if provided.\r
1903 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r
1904 of the command line arguments was returned in \r
1905 ProblemParam if provided.\r
1906 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1907 the invalid command line argument was returned in\r
1908 ProblemParam if provided.\r
1909**/\r
1910EFI_STATUS\r
1911EFIAPI\r
2247dde4 1912ShellCommandLineParseEx (\r
94b17fa1 1913 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1914 OUT LIST_ENTRY **CheckPackage,\r
1915 OUT CHAR16 **ProblemParam OPTIONAL,\r
2247dde4 1916 IN BOOLEAN AutoPageBreak,\r
1917 IN BOOLEAN AlwaysAllowNumbers\r
1918 ) {\r
94b17fa1 1919 // \r
1920 // ASSERT that CheckList and CheckPackage aren't NULL\r
1921 //\r
1922 ASSERT(CheckList != NULL);\r
1923 ASSERT(CheckPackage != NULL);\r
1924\r
1925 // \r
1926 // Check for UEFI Shell 2.0 protocols\r
1927 //\r
1928 if (mEfiShellParametersProtocol != NULL) {\r
1929 return (InternalCommandLineParse(CheckList, \r
1930 CheckPackage, \r
1931 ProblemParam, \r
1932 AutoPageBreak, \r
08d7f8e8 1933 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
2247dde4 1934 mEfiShellParametersProtocol->Argc,\r
1935 AlwaysAllowNumbers));\r
94b17fa1 1936 }\r
1937\r
1938 // \r
1939 // ASSERT That EFI Shell is not required\r
1940 //\r
1941 ASSERT (mEfiShellInterface != NULL);\r
1942 return (InternalCommandLineParse(CheckList, \r
1943 CheckPackage, \r
1944 ProblemParam, \r
1945 AutoPageBreak, \r
08d7f8e8 1946 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2247dde4 1947 mEfiShellInterface->Argc,\r
1948 AlwaysAllowNumbers));\r
94b17fa1 1949}\r
1950\r
1951/**\r
1952 Frees shell variable list that was returned from ShellCommandLineParse.\r
1953\r
1954 This function will free all the memory that was used for the CheckPackage\r
1955 list of postprocessed shell arguments.\r
1956\r
1957 this function has no return value.\r
1958\r
1959 if CheckPackage is NULL, then return\r
1960\r
1961 @param CheckPackage the list to de-allocate\r
1962 **/\r
1963VOID\r
1964EFIAPI\r
1965ShellCommandLineFreeVarList (\r
1966 IN LIST_ENTRY *CheckPackage\r
2247dde4 1967 ) {\r
94b17fa1 1968 LIST_ENTRY *Node;\r
1969\r
1970 //\r
1971 // check for CheckPackage == NULL\r
1972 //\r
1973 if (CheckPackage == NULL) {\r
1974 return;\r
1975 }\r
1976\r
1977 //\r
1978 // for each node in the list\r
1979 //\r
9eb53ac3 1980 for ( Node = GetFirstNode(CheckPackage)\r
2247dde4 1981 ; IsListEmpty(CheckPackage) == FALSE\r
9eb53ac3 1982 ; Node = GetFirstNode(CheckPackage)\r
1983 ){\r
94b17fa1 1984 //\r
1985 // Remove it from the list\r
1986 //\r
1987 RemoveEntryList(Node);\r
1988\r
1989 //\r
1990 // if it has a name free the name\r
1991 //\r
1992 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
1993 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
1994 }\r
1995\r
1996 //\r
1997 // if it has a value free the value\r
1998 //\r
1999 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2000 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2001 }\r
2002 \r
2003 //\r
2004 // free the node structure\r
2005 //\r
2006 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2007 }\r
2008 //\r
2009 // free the list head node\r
2010 //\r
2011 FreePool(CheckPackage);\r
2012}\r
2013/**\r
2014 Checks for presence of a flag parameter\r
2015\r
2016 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2017\r
2018 if CheckPackage is NULL then return FALSE.\r
2019 if KeyString is NULL then ASSERT()\r
2020 \r
2021 @param CheckPackage The package of parsed command line arguments\r
2022 @param KeyString the Key of the command line argument to check for\r
2023\r
2024 @retval TRUE the flag is on the command line\r
2025 @retval FALSE the flag is not on the command line\r
2026 **/\r
2027BOOLEAN\r
2028EFIAPI\r
2029ShellCommandLineGetFlag (\r
2030 IN CONST LIST_ENTRY *CheckPackage,\r
2031 IN CHAR16 *KeyString\r
2247dde4 2032 ) {\r
94b17fa1 2033 LIST_ENTRY *Node;\r
2034\r
2035 //\r
2036 // ASSERT that both CheckPackage and KeyString aren't NULL\r
2037 //\r
2038 ASSERT(KeyString != NULL);\r
2039\r
2040 //\r
2041 // return FALSE for no package\r
2042 //\r
2043 if (CheckPackage == NULL) {\r
2044 return (FALSE);\r
2045 }\r
2046\r
2047 //\r
2048 // enumerate through the list of parametrs\r
2049 //\r
9eb53ac3 2050 for ( Node = GetFirstNode(CheckPackage) \r
2051 ; !IsNull (CheckPackage, Node) \r
2052 ; Node = GetNextNode(CheckPackage, Node) \r
2053 ){\r
94b17fa1 2054 //\r
2055 // If the Name matches, return TRUE (and there may be NULL name)\r
2056 //\r
2057 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2058 //\r
2059 // If Type is TypeStart then only compare the begining of the strings\r
2060 //\r
2061 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
2062 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2063 ){\r
2064 return (TRUE);\r
2065 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2066 return (TRUE);\r
2067 }\r
2068 }\r
2069 }\r
2070 return (FALSE);\r
2071}\r
2072/**\r
2073 returns value from command line argument\r
2074\r
2075 value parameters are in the form of "-<Key> value" or "/<Key> value"\r
2076 \r
2077 if CheckPackage is NULL, then return NULL;\r
2078\r
2079 @param CheckPackage The package of parsed command line arguments\r
2080 @param KeyString the Key of the command line argument to check for\r
2081\r
2082 @retval NULL the flag is not on the command line\r
2083 @return !=NULL pointer to unicode string of the value\r
2084 **/\r
2085CONST CHAR16*\r
2086EFIAPI\r
2087ShellCommandLineGetValue (\r
2088 IN CONST LIST_ENTRY *CheckPackage,\r
2089 IN CHAR16 *KeyString\r
2247dde4 2090 ) {\r
94b17fa1 2091 LIST_ENTRY *Node;\r
2092\r
2093 //\r
2094 // check for CheckPackage == NULL\r
2095 //\r
2096 if (CheckPackage == NULL) {\r
2097 return (NULL);\r
2098 }\r
2099\r
2100 //\r
2101 // enumerate through the list of parametrs\r
2102 //\r
9eb53ac3 2103 for ( Node = GetFirstNode(CheckPackage) \r
2104 ; !IsNull (CheckPackage, Node) \r
2105 ; Node = GetNextNode(CheckPackage, Node) \r
2106 ){\r
94b17fa1 2107 //\r
2108 // If the Name matches, return the value (name can be NULL)\r
2109 //\r
2110 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2111 //\r
2112 // If Type is TypeStart then only compare the begining of the strings\r
2113 //\r
2114 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
2115 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2116 ){\r
2117 //\r
2118 // return the string part after the flag\r
2119 //\r
2120 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2121 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2122 //\r
2123 // return the value\r
2124 //\r
94b17fa1 2125 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2126 }\r
2127 }\r
2128 }\r
2129 return (NULL);\r
2130}\r
2131/**\r
2132 returns raw value from command line argument\r
2133\r
2134 raw value parameters are in the form of "value" in a specific position in the list\r
2135 \r
2136 if CheckPackage is NULL, then return NULL;\r
2137\r
2138 @param CheckPackage The package of parsed command line arguments\r
2139 @param Position the position of the value \r
2140\r
2141 @retval NULL the flag is not on the command line\r
2142 @return !=NULL pointer to unicode string of the value\r
2143 **/\r
2144CONST CHAR16*\r
2145EFIAPI\r
2146ShellCommandLineGetRawValue (\r
2147 IN CONST LIST_ENTRY *CheckPackage,\r
2148 IN UINT32 Position\r
2247dde4 2149 ) {\r
94b17fa1 2150 LIST_ENTRY *Node;\r
2151\r
2152 //\r
2153 // check for CheckPackage == NULL\r
2154 //\r
2155 if (CheckPackage == NULL) {\r
2156 return (NULL);\r
2157 }\r
2158\r
2159 //\r
2160 // enumerate through the list of parametrs\r
2161 //\r
b82bfcc1 2162 for ( Node = GetFirstNode(CheckPackage) \r
2163 ; !IsNull (CheckPackage, Node) \r
2164 ; Node = GetNextNode(CheckPackage, Node) \r
2165 ){\r
94b17fa1 2166 //\r
2167 // If the position matches, return the value\r
2168 //\r
2169 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2170 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2171 }\r
2172 }\r
2173 return (NULL);\r
b1f95a06 2174}\r
2247dde4 2175\r
2176/**\r
2177 returns the number of command line value parameters that were parsed. \r
2178 \r
2179 this will not include flags.\r
2180\r
2181 @retval (UINTN)-1 No parsing has ocurred\r
2182 @return other The number of value parameters found\r
2183**/\r
2184UINTN\r
2185EFIAPI\r
2186ShellCommandLineGetCount(\r
2187 VOID\r
125c2cf4 2188 )\r
2189{\r
2247dde4 2190 return (mTotalParameterCount);\r
2191}\r
2192\r
36a9d672 2193/**\r
2194 Determins if a parameter is duplicated.\r
2195\r
2196 If Param is not NULL then it will point to a callee allocated string buffer \r
2197 with the parameter value if a duplicate is found.\r
2198\r
2199 If CheckPackage is NULL, then ASSERT.\r
2200\r
2201 @param[in] CheckPackage The package of parsed command line arguments.\r
2202 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2203\r
2204 @retval EFI_SUCCESS No parameters were duplicated.\r
2205 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2206 **/\r
2207EFI_STATUS\r
2208EFIAPI\r
2209ShellCommandLineCheckDuplicate (\r
2210 IN CONST LIST_ENTRY *CheckPackage,\r
2211 OUT CHAR16 **Param\r
2212 )\r
2213{\r
2214 LIST_ENTRY *Node1;\r
2215 LIST_ENTRY *Node2;\r
2216 \r
2217 ASSERT(CheckPackage != NULL);\r
2218\r
2219 for ( Node1 = GetFirstNode(CheckPackage) \r
2220 ; !IsNull (CheckPackage, Node1) \r
2221 ; Node1 = GetNextNode(CheckPackage, Node1) \r
2222 ){\r
2223 for ( Node2 = GetNextNode(CheckPackage, Node1) \r
2224 ; !IsNull (CheckPackage, Node2) \r
2225 ; Node2 = GetNextNode(CheckPackage, Node2) \r
2226 ){\r
2227 if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r
2228 if (Param != NULL) {\r
2229 *Param = NULL;\r
2230 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2231 }\r
2232 return (EFI_DEVICE_ERROR);\r
2233 }\r
2234 }\r
2235 }\r
2236 return (EFI_SUCCESS);\r
2237}\r
2238\r
975136ab 2239/**\r
b3011f40 2240 This is a find and replace function. Upon successful return the NewString is a copy of \r
975136ab 2241 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2242\r
b3011f40 2243 If SourceString and NewString overlap the behavior is undefined.\r
2244\r
975136ab 2245 If the string would grow bigger than NewSize it will halt and return error.\r
2246\r
2247 @param[in] SourceString String with source buffer\r
b82bfcc1 2248 @param[in,out] NewString String with resultant buffer\r
975136ab 2249 @param[in] NewSize Size in bytes of NewString\r
2250 @param[in] FindTarget String to look for\r
2251 @param[in[ ReplaceWith String to replace FindTarget with\r
2252\r
2253 @retval EFI_INVALID_PARAMETER SourceString was NULL\r
2254 @retval EFI_INVALID_PARAMETER NewString was NULL\r
2255 @retval EFI_INVALID_PARAMETER FindTarget was NULL\r
2256 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL\r
2257 @retval EFI_INVALID_PARAMETER FindTarget had length < 1\r
2258 @retval EFI_INVALID_PARAMETER SourceString had length < 1\r
2259 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold \r
2260 the new string (truncation occurred)\r
2261 @retval EFI_SUCCESS the string was sucessfully copied with replacement\r
2262**/\r
2263\r
2264EFI_STATUS\r
2265EFIAPI\r
b3011f40 2266ShellLibCopySearchAndReplace(\r
975136ab 2267 IN CHAR16 CONST *SourceString,\r
2268 IN CHAR16 *NewString,\r
2269 IN UINTN NewSize,\r
2270 IN CONST CHAR16 *FindTarget,\r
2271 IN CONST CHAR16 *ReplaceWith\r
2247dde4 2272 ) \r
2273{\r
0158294b 2274 UINTN Size;\r
975136ab 2275 if ( (SourceString == NULL)\r
2276 || (NewString == NULL)\r
2277 || (FindTarget == NULL)\r
2278 || (ReplaceWith == NULL)\r
2279 || (StrLen(FindTarget) < 1)\r
2280 || (StrLen(SourceString) < 1)\r
2281 ){\r
2282 return (EFI_INVALID_PARAMETER);\r
2283 }\r
2247dde4 2284 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2285 while (*SourceString != CHAR_NULL) {\r
975136ab 2286 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {\r
2287 SourceString += StrLen(FindTarget);\r
0158294b 2288 Size = StrSize(NewString);\r
2289 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r
975136ab 2290 return (EFI_BUFFER_TOO_SMALL);\r
2291 }\r
2292 StrCat(NewString, ReplaceWith);\r
2293 } else {\r
0158294b 2294 Size = StrSize(NewString);\r
2295 if (Size + sizeof(CHAR16) > NewSize) {\r
975136ab 2296 return (EFI_BUFFER_TOO_SMALL);\r
2297 }\r
2298 StrnCat(NewString, SourceString, 1);\r
2299 SourceString++;\r
2300 }\r
2301 }\r
2302 return (EFI_SUCCESS);\r
2303}\r
b1f95a06 2304\r
e2f8297f 2305/**\r
2306 Internal worker function to output a string.\r
2307\r
2308 This function will output a string to the correct StdOut.\r
2309\r
2310 @param[in] String The string to print out.\r
2311\r
2312 @retval EFI_SUCCESS The operation was sucessful.\r
2313 @retval !EFI_SUCCESS The operation failed.\r
2314**/\r
2315EFI_STATUS\r
2316EFIAPI\r
2317InternalPrintTo (\r
2318 IN CONST CHAR16 *String\r
2319 )\r
2320{\r
2321 UINTN Size;\r
2322 Size = StrSize(String) - sizeof(CHAR16);\r
2323 if (mEfiShellParametersProtocol != NULL) {\r
2324 return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
2325 }\r
2326 if (mEfiShellInterface != NULL) {\r
ecd3d59f 2327 //\r
2328 // Divide in half for old shell. Must be string length not size.\r
2329 //\r
2330 Size /= 2;\r
e2f8297f 2331 return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
2332 }\r
2333 ASSERT(FALSE);\r
2334 return (EFI_UNSUPPORTED);\r
2335}\r
2336\r
b1f95a06 2337/**\r
2338 Print at a specific location on the screen.\r
2339\r
f1b87e7a 2340 This function will move the cursor to a given screen location and print the specified string\r
b1f95a06 2341 \r
2342 If -1 is specified for either the Row or Col the current screen location for BOTH \r
f1b87e7a 2343 will be used.\r
b1f95a06 2344\r
2345 if either Row or Col is out of range for the current console, then ASSERT\r
2346 if Format is NULL, then ASSERT\r
2347\r
2348 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2349 the following additional flags:\r
2350 %N - Set output attribute to normal\r
2351 %H - Set output attribute to highlight\r
2352 %E - Set output attribute to error\r
2353 %B - Set output attribute to blue color\r
2354 %V - Set output attribute to green color\r
2355\r
2356 Note: The background color is controlled by the shell command cls.\r
2357\r
2358 @param[in] Row the row to print at\r
2359 @param[in] Col the column to print at\r
2360 @param[in] Format the format string\r
2247dde4 2361 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2362\r
2363 @return the number of characters printed to the screen\r
2364**/\r
2365\r
2366UINTN\r
2367EFIAPI\r
2247dde4 2368InternalShellPrintWorker(\r
b1f95a06 2369 IN INT32 Col OPTIONAL,\r
2370 IN INT32 Row OPTIONAL,\r
2371 IN CONST CHAR16 *Format,\r
2247dde4 2372 VA_LIST Marker\r
2373 ) \r
2374{\r
b1f95a06 2375 UINTN Return;\r
b1f95a06 2376 EFI_STATUS Status;\r
975136ab 2377 UINTN NormalAttribute;\r
2378 CHAR16 *ResumeLocation;\r
2379 CHAR16 *FormatWalker;\r
b1f95a06 2380 \r
975136ab 2381 //\r
2382 // Back and forth each time fixing up 1 of our flags...\r
2383 //\r
b3011f40 2384 Status = ShellLibCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");\r
975136ab 2385 ASSERT_EFI_ERROR(Status);\r
b3011f40 2386 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");\r
975136ab 2387 ASSERT_EFI_ERROR(Status);\r
b3011f40 2388 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");\r
975136ab 2389 ASSERT_EFI_ERROR(Status);\r
b3011f40 2390 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");\r
975136ab 2391 ASSERT_EFI_ERROR(Status);\r
b3011f40 2392 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");\r
975136ab 2393 ASSERT_EFI_ERROR(Status);\r
2394\r
2395 //\r
2396 // Use the last buffer from replacing to print from...\r
2397 //\r
b3011f40 2398 Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
b1f95a06 2399\r
2400 if (Col != -1 && Row != -1) {\r
b1f95a06 2401 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2402 ASSERT_EFI_ERROR(Status);\r
975136ab 2403 }\r
2404\r
2405 NormalAttribute = gST->ConOut->Mode->Attribute;\r
ecd3d59f 2406 FormatWalker = mPostReplaceFormat2;\r
2247dde4 2407 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2408 //\r
2409 // Find the next attribute change request\r
2410 //\r
2411 ResumeLocation = StrStr(FormatWalker, L"%");\r
2412 if (ResumeLocation != NULL) {\r
2247dde4 2413 *ResumeLocation = CHAR_NULL;\r
975136ab 2414 }\r
2415 //\r
2416 // print the current FormatWalker string\r
2417 //\r
e2f8297f 2418 Status = InternalPrintTo(FormatWalker);\r
b1f95a06 2419 ASSERT_EFI_ERROR(Status);\r
975136ab 2420 //\r
2421 // update the attribute\r
2422 //\r
2423 if (ResumeLocation != NULL) {\r
2424 switch (*(ResumeLocation+1)) {\r
2425 case (L'N'):\r
2426 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2427 break;\r
2428 case (L'E'):\r
2429 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2430 break;\r
2431 case (L'H'):\r
2432 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2433 break;\r
2434 case (L'B'):\r
2435 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2436 break;\r
2437 case (L'V'):\r
2438 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2439 break;\r
2440 default:\r
e2f8297f 2441 //\r
2442 // Print a simple '%' symbol\r
2443 //\r
2444 Status = InternalPrintTo(L"%");\r
2445 ASSERT_EFI_ERROR(Status);\r
2446 ResumeLocation = ResumeLocation - 1;\r
975136ab 2447 break;\r
2448 }\r
2449 } else {\r
2450 //\r
2451 // reset to normal now...\r
2452 //\r
2453 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2454 break;\r
2455 }\r
2456\r
2457 //\r
2458 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2459 //\r
2460 FormatWalker = ResumeLocation + 2;\r
2461 }\r
b1f95a06 2462\r
b1f95a06 2463 return (Return);\r
5f7431d0 2464}\r
2247dde4 2465\r
2466/**\r
2467 Print at a specific location on the screen.\r
2468\r
e2f8297f 2469 This function will move the cursor to a given screen location and print the specified string.\r
2247dde4 2470 \r
2471 If -1 is specified for either the Row or Col the current screen location for BOTH \r
2472 will be used.\r
2473\r
e2f8297f 2474 If either Row or Col is out of range for the current console, then ASSERT.\r
2475 If Format is NULL, then ASSERT.\r
2247dde4 2476\r
2477 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2478 the following additional flags:\r
2479 %N - Set output attribute to normal\r
2480 %H - Set output attribute to highlight\r
2481 %E - Set output attribute to error\r
2482 %B - Set output attribute to blue color\r
2483 %V - Set output attribute to green color\r
2484\r
2485 Note: The background color is controlled by the shell command cls.\r
2486\r
2487 @param[in] Row the row to print at\r
2488 @param[in] Col the column to print at\r
2489 @param[in] Format the format string\r
2490\r
2491 @return the number of characters printed to the screen\r
2492**/\r
2493\r
2494UINTN\r
2495EFIAPI\r
2496ShellPrintEx(\r
2497 IN INT32 Col OPTIONAL,\r
2498 IN INT32 Row OPTIONAL,\r
2499 IN CONST CHAR16 *Format,\r
2500 ...\r
2501 ) \r
2502{\r
2503 VA_LIST Marker;\r
e2f8297f 2504 EFI_STATUS Status;\r
2247dde4 2505 VA_START (Marker, Format);\r
e2f8297f 2506 Status = InternalShellPrintWorker(Col, Row, Format, Marker);\r
2507 VA_END(Marker);\r
2508 return(Status);\r
2247dde4 2509}\r
2510\r
2511/**\r
2512 Print at a specific location on the screen.\r
2513\r
e2f8297f 2514 This function will move the cursor to a given screen location and print the specified string.\r
2247dde4 2515 \r
2516 If -1 is specified for either the Row or Col the current screen location for BOTH \r
e2f8297f 2517 will be used.\r
2247dde4 2518\r
e2f8297f 2519 If either Row or Col is out of range for the current console, then ASSERT.\r
2520 If Format is NULL, then ASSERT.\r
2247dde4 2521\r
2522 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2523 the following additional flags:\r
2524 %N - Set output attribute to normal\r
2525 %H - Set output attribute to highlight\r
2526 %E - Set output attribute to error\r
2527 %B - Set output attribute to blue color\r
2528 %V - Set output attribute to green color\r
2529\r
2530 Note: The background color is controlled by the shell command cls.\r
2531\r
2532 @param[in] Row the row to print at\r
2533 @param[in] Col the column to print at\r
2534 @param[in] HiiFormatStringId the format string Id for getting from Hii\r
2535 @param[in] HiiFormatHandle the format string Handle for getting from Hii\r
2536\r
2537 @return the number of characters printed to the screen\r
2538**/\r
2539UINTN\r
2540EFIAPI\r
2541ShellPrintHiiEx(\r
2542 IN INT32 Col OPTIONAL,\r
2543 IN INT32 Row OPTIONAL,\r
2544 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2545 IN CONST EFI_HANDLE HiiFormatHandle,\r
2546 ...\r
2547 )\r
2548{\r
2549 VA_LIST Marker;\r
2550 CHAR16 *HiiFormatString;\r
2551 UINTN RetVal;\r
2552\r
2553 VA_START (Marker, HiiFormatHandle);\r
2554 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
2555 ASSERT(HiiFormatString != NULL);\r
2556\r
2557 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2558\r
2559 FreePool(HiiFormatString);\r
e2f8297f 2560 VA_END(Marker);\r
2247dde4 2561\r
2562 return (RetVal);\r
2563}\r
2564\r
2565/**\r
2566 Function to determine if a given filename represents a file or a directory.\r
2567\r
2568 @param[in] DirName Path to directory to test.\r
2569\r
2570 @retval EFI_SUCCESS The Path represents a directory\r
2571 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2572 @return other The path failed to open\r
2573**/\r
2574EFI_STATUS\r
2575EFIAPI\r
2576ShellIsDirectory(\r
2577 IN CONST CHAR16 *DirName\r
2578 )\r
2579{\r
2580 EFI_STATUS Status;\r
2581 EFI_FILE_HANDLE Handle;\r
2582\r
ecd3d59f 2583 ASSERT(DirName != NULL);\r
2584\r
2247dde4 2585 Handle = NULL;\r
2586\r
2587 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2588 if (EFI_ERROR(Status)) {\r
2589 return (Status);\r
2590 }\r
2591\r
2592 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2593 ShellCloseFile(&Handle);\r
2594 return (EFI_SUCCESS);\r
2595 }\r
2596 ShellCloseFile(&Handle);\r
2597 return (EFI_NOT_FOUND);\r
2598}\r
2599\r
36a9d672 2600/**\r
2601 Function to determine if a given filename represents a file.\r
2602\r
2603 @param[in] Name Path to file to test.\r
2604\r
2605 @retval EFI_SUCCESS The Path represents a file.\r
2606 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2607 @retval other The path failed to open.\r
2608**/\r
2609EFI_STATUS\r
2610EFIAPI\r
2611ShellIsFile(\r
2612 IN CONST CHAR16 *Name\r
2613 )\r
2614{\r
2615 EFI_STATUS Status;\r
2616 EFI_FILE_HANDLE Handle;\r
2617\r
ecd3d59f 2618 ASSERT(Name != NULL);\r
2619\r
36a9d672 2620 Handle = NULL;\r
2621\r
2622 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2623 if (EFI_ERROR(Status)) {\r
2624 return (Status);\r
2625 }\r
2626\r
2627 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2628 ShellCloseFile(&Handle);\r
2629 return (EFI_SUCCESS);\r
2630 }\r
2631 ShellCloseFile(&Handle);\r
2632 return (EFI_NOT_FOUND);\r
2633}\r
2634\r
b3011f40 2635/**\r
2636 Function to determine if a given filename represents a file.\r
2637\r
2638 This will search the CWD and then the Path.\r
2639\r
2640 If Name is NULL, then ASSERT.\r
2641\r
2642 @param[in] Name Path to file to test.\r
2643\r
2644 @retval EFI_SUCCESS The Path represents a file.\r
2645 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2646 @retval other The path failed to open.\r
2647**/\r
2648EFI_STATUS\r
2649EFIAPI\r
2650ShellIsFileInPath(\r
2651 IN CONST CHAR16 *Name\r
2652 ) {\r
2653 CHAR16 *NewName;\r
2654 EFI_STATUS Status;\r
2655\r
2656 if (!EFI_ERROR(ShellIsFile(Name))) {\r
2657 return (TRUE);\r
2658 }\r
2659\r
2660 NewName = ShellFindFilePath(Name);\r
2661 if (NewName == NULL) {\r
2662 return (EFI_NOT_FOUND);\r
2663 }\r
2664 Status = ShellIsFile(NewName);\r
2665 FreePool(NewName);\r
2666 return (Status);\r
2667}\r
125c2cf4 2668/**\r
2669 Function to determine whether a string is decimal or hex representation of a number \r
2670 and return the number converted from the string.\r
2671\r
2672 @param[in] String String representation of a number\r
2673\r
2674 @retval all the number\r
2675**/\r
2676UINTN\r
2677EFIAPI\r
2678ShellStrToUintn(\r
2679 IN CONST CHAR16 *String\r
2680 )\r
2681{\r
2682 CONST CHAR16 *Walker;\r
b3011f40 2683 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
125c2cf4 2684 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
2685 return (StrHexToUintn(Walker));\r
2686 }\r
2687 return (StrDecimalToUintn(Walker));\r
2688}\r
2689\r
2690/**\r
2691 Safely append with automatic string resizing given length of Destination and\r
2692 desired length of copy from Source.\r
2693\r
2694 append the first D characters of Source to the end of Destination, where D is\r
2695 the lesser of Count and the StrLen() of Source. If appending those D characters\r
2696 will fit within Destination (whose Size is given as CurrentSize) and\r
2697 still leave room for a null terminator, then those characters are appended,\r
2698 starting at the original terminating null of Destination, and a new terminating\r
2699 null is appended.\r
2700\r
2701 If appending D characters onto Destination will result in a overflow of the size\r
2702 given in CurrentSize the string will be grown such that the copy can be performed\r
2703 and CurrentSize will be updated to the new size.\r
2704\r
2705 If Source is NULL, there is nothing to append, just return the current buffer in\r
2706 Destination.\r
2707\r
2708 if Destination is NULL, then ASSERT()\r
2709 if Destination's current length (including NULL terminator) is already more then\r
2710 CurrentSize, then ASSERT()\r
2711\r
2712 @param[in,out] Destination The String to append onto\r
2713 @param[in,out] CurrentSize on call the number of bytes in Destination. On\r
2714 return possibly the new size (still in bytes). if NULL\r
2715 then allocate whatever is needed.\r
2716 @param[in] Source The String to append from\r
2717 @param[in] Count Maximum number of characters to append. if 0 then\r
2718 all are appended.\r
2719\r
2720 @return Destination return the resultant string.\r
2721**/\r
2722CHAR16*\r
2723EFIAPI\r
2724StrnCatGrow (\r
2725 IN OUT CHAR16 **Destination,\r
2726 IN OUT UINTN *CurrentSize,\r
2727 IN CONST CHAR16 *Source,\r
2728 IN UINTN Count\r
2729 )\r
2730{\r
2731 UINTN DestinationStartSize;\r
2732 UINTN NewSize;\r
2733\r
2734 //\r
2735 // ASSERTs\r
2736 //\r
2737 ASSERT(Destination != NULL);\r
2738\r
2739 //\r
2740 // If there's nothing to do then just return Destination\r
2741 //\r
2742 if (Source == NULL) {\r
2743 return (*Destination);\r
2744 }\r
2745\r
2746 //\r
2747 // allow for un-initialized pointers, based on size being 0\r
2748 //\r
2749 if (CurrentSize != NULL && *CurrentSize == 0) {\r
2750 *Destination = NULL;\r
2751 }\r
2752\r
2753 //\r
2754 // allow for NULL pointers address as Destination\r
2755 //\r
2756 if (*Destination != NULL) {\r
2757 ASSERT(CurrentSize != 0);\r
2758 DestinationStartSize = StrSize(*Destination);\r
2759 ASSERT(DestinationStartSize <= *CurrentSize);\r
2760 } else {\r
2761 DestinationStartSize = 0;\r
2762// ASSERT(*CurrentSize == 0);\r
2763 }\r
2764\r
2765 //\r
2766 // Append all of Source?\r
2767 //\r
2768 if (Count == 0) {\r
2769 Count = StrLen(Source);\r
2770 }\r
2771\r
2772 //\r
2773 // Test and grow if required\r
2774 //\r
2775 if (CurrentSize != NULL) {\r
2776 NewSize = *CurrentSize;\r
2777 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
2778 NewSize += 2 * Count * sizeof(CHAR16);\r
2779 }\r
2780 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
2781 *CurrentSize = NewSize;\r
2782 } else {\r
2783 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
2784 }\r
2785\r
2786 //\r
2787 // Now use standard StrnCat on a big enough buffer\r
2788 //\r
2789 return StrnCat(*Destination, Source, Count);\r
2790}\r