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