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