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