]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
fixed operator order
[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
5f7431d0 1305 If you are NOT appending to an existing list *ListHead must be NULL. If \r
1306 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1307\r
1308 @param Arg pointer to path string\r
1309 @param OpenMode mode to open files with\r
1310 @param ListHead head of linked list of results\r
1311\r
1312 @retval EFI_SUCCESS the operation was sucessful and the list head \r
1313 contains the list of opened files\r
1314 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.\r
1315 *ListHead is set to NULL.\r
1316 @return != EFI_SUCCESS the operation failed\r
1317\r
1318 @sa InternalShellConvertFileListType\r
1319**/\r
1320EFI_STATUS\r
1321EFIAPI\r
1322ShellOpenFileMetaArg (\r
1323 IN CHAR16 *Arg,\r
1324 IN UINT64 OpenMode,\r
1325 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1326 )\r
1327{\r
1328 EFI_STATUS Status;\r
9b3bf083 1329 LIST_ENTRY mOldStyleFileList;\r
d2b4564b 1330 \r
94b17fa1 1331 //\r
1332 // ASSERT that Arg and ListHead are not NULL\r
1333 //\r
1334 ASSERT(Arg != NULL);\r
1335 ASSERT(ListHead != NULL);\r
1336\r
1337 // \r
1338 // Check for UEFI Shell 2.0 protocols\r
1339 //\r
1340 if (mEfiShellProtocol != NULL) {\r
5f7431d0 1341 if (*ListHead == NULL) {\r
1342 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1343 if (*ListHead == NULL) {\r
1344 return (EFI_OUT_OF_RESOURCES);\r
1345 }\r
1346 InitializeListHead(&((*ListHead)->Link));\r
1347 } \r
94b17fa1 1348 return (mEfiShellProtocol->OpenFileList(Arg, \r
1349 OpenMode, \r
1350 ListHead));\r
1351 } \r
1352\r
1353 //\r
1354 // ASSERT that we must have EFI shell\r
1355 //\r
1356 ASSERT(mEfiShellEnvironment2 != NULL);\r
1357\r
94b17fa1 1358 //\r
1359 // make sure the list head is initialized\r
1360 //\r
9b3bf083 1361 InitializeListHead(&mOldStyleFileList);\r
94b17fa1 1362\r
1363 //\r
1364 // Get the EFI Shell list of files\r
1365 //\r
9b3bf083 1366 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
94b17fa1 1367 if (EFI_ERROR(Status)) {\r
1368 *ListHead = NULL;\r
1369 return (Status);\r
1370 }\r
1371\r
9b3bf083 1372 if (*ListHead == NULL) {\r
1373 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1374 if (*ListHead == NULL) {\r
1375 return (EFI_OUT_OF_RESOURCES);\r
1376 }\r
1377 }\r
1378\r
94b17fa1 1379 //\r
1380 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1381 //\r
9b3bf083 1382 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1383\r
d2b4564b 1384 //\r
1385 // Free the EFI Shell version that was converted.\r
1386 //\r
9b3bf083 1387 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
94b17fa1 1388\r
1389 return (Status);\r
1390}\r
1391/**\r
1392 Free the linked list returned from ShellOpenFileMetaArg\r
1393\r
1394 if ListHead is NULL then ASSERT()\r
1395\r
1396 @param ListHead the pointer to free\r
1397\r
1398 @retval EFI_SUCCESS the operation was sucessful\r
1399**/\r
1400EFI_STATUS\r
1401EFIAPI\r
1402ShellCloseFileMetaArg (\r
1403 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1404 )\r
1405{\r
1406 LIST_ENTRY *Node;\r
1407\r
1408 //\r
1409 // ASSERT that ListHead is not NULL\r
1410 //\r
1411 ASSERT(ListHead != NULL);\r
1412\r
1413 // \r
1414 // Check for UEFI Shell 2.0 protocols\r
1415 //\r
1416 if (mEfiShellProtocol != NULL) {\r
1417 return (mEfiShellProtocol->FreeFileList(ListHead));\r
1418 } else {\r
94b17fa1 1419 //\r
1420 // Since this is EFI Shell version we need to free our internally made copy \r
1421 // of the list\r
1422 //\r
9b3bf083 1423 for ( Node = GetFirstNode(&(*ListHead)->Link) \r
1424 ; IsListEmpty(&(*ListHead)->Link) == FALSE \r
1425 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
94b17fa1 1426 RemoveEntryList(Node);\r
d2b4564b 1427 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
94b17fa1 1428 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1429 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1430 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1431 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1432 }\r
1433 return EFI_SUCCESS;\r
1434 }\r
1435}\r
1436\r
1437typedef struct {\r
9b3bf083 1438 LIST_ENTRY Link;\r
94b17fa1 1439 CHAR16 *Name;\r
1440 ParamType Type;\r
1441 CHAR16 *Value;\r
1442 UINTN OriginalPosition;\r
1443} SHELL_PARAM_PACKAGE;\r
1444\r
1445/**\r
1446 Checks the list of valid arguments and returns TRUE if the item was found. If the \r
1447 return value is TRUE then the type parameter is set also.\r
1448 \r
1449 if CheckList is NULL then ASSERT();\r
1450 if Name is NULL then ASSERT();\r
1451 if Type is NULL then ASSERT();\r
1452\r
1453 @param Type pointer to type of parameter if it was found\r
1454 @param Name pointer to Name of parameter found\r
1455 @param CheckList List to check against\r
1456\r
1457 @retval TRUE the Parameter was found. Type is valid.\r
1458 @retval FALSE the Parameter was not found. Type is not valid.\r
1459**/\r
1460BOOLEAN\r
1461EFIAPI\r
d2b4564b 1462InternalIsOnCheckList (\r
94b17fa1 1463 IN CONST CHAR16 *Name,\r
1464 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1465 OUT ParamType *Type\r
1466 )\r
1467{\r
1468 SHELL_PARAM_ITEM *TempListItem;\r
1469\r
1470 //\r
1471 // ASSERT that all 3 pointer parameters aren't NULL\r
1472 //\r
1473 ASSERT(CheckList != NULL);\r
1474 ASSERT(Type != NULL);\r
1475 ASSERT(Name != NULL);\r
1476\r
d2b4564b 1477 //\r
1478 // question mark and page break mode are always supported\r
1479 //\r
1480 if ((StrCmp(Name, L"-?") == 0) ||\r
1481 (StrCmp(Name, L"-b") == 0)\r
1482 ) {\r
1483 return (TRUE);\r
1484 }\r
1485\r
94b17fa1 1486 //\r
1487 // Enumerate through the list\r
1488 //\r
1489 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1490 //\r
9eb53ac3 1491 // If the Type is TypeStart only check the first characters of the passed in param\r
1492 // If it matches set the type and return TRUE\r
94b17fa1 1493 //\r
9eb53ac3 1494 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1495 *Type = TempListItem->Type;\r
1496 return (TRUE);\r
1497 } else if (StrCmp(Name, TempListItem->Name) == 0) {\r
94b17fa1 1498 *Type = TempListItem->Type;\r
1499 return (TRUE);\r
1500 }\r
1501 }\r
1502 return (FALSE);\r
1503}\r
1504/**\r
d2b4564b 1505 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 1506\r
1507 @param Name pointer to Name of parameter found\r
1508\r
1509 @retval TRUE the Parameter is a flag.\r
1510 @retval FALSE the Parameter not a flag\r
1511**/\r
1512BOOLEAN\r
1513EFIAPI\r
d2b4564b 1514InternalIsFlag (\r
94b17fa1 1515 IN CONST CHAR16 *Name\r
1516 )\r
1517{\r
1518 //\r
1519 // ASSERT that Name isn't NULL\r
1520 //\r
1521 ASSERT(Name != NULL);\r
1522\r
1523 //\r
1524 // If the Name has a / or - as the first character return TRUE\r
1525 //\r
d2b4564b 1526 if ((Name[0] == L'/') || \r
1527 (Name[0] == L'-') ||\r
1528 (Name[0] == L'+')\r
1529 ) {\r
94b17fa1 1530 return (TRUE);\r
1531 }\r
1532 return (FALSE);\r
1533}\r
1534\r
1535/**\r
1536 Checks the command line arguments passed against the list of valid ones. \r
1537\r
1538 If no initialization is required, then return RETURN_SUCCESS.\r
1539 \r
1540 @param CheckList pointer to list of parameters to check\r
1541 @param CheckPackage pointer to pointer to list checked values\r
1542 @param ProblemParam optional pointer to pointer to unicode string for \r
d2b4564b 1543 the paramater that caused failure. If used then the\r
1544 caller is responsible for freeing the memory.\r
94b17fa1 1545 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1546 @param Argc Count of parameters in Argv\r
1547 @param Argv pointer to array of parameters\r
1548\r
1549 @retval EFI_SUCCESS The operation completed sucessfully.\r
1550 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1551 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1552 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1553 duplicated. the duplicated command line argument \r
1554 was returned in ProblemParam if provided.\r
1555 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1556 the invalid command line argument was returned in\r
1557 ProblemParam if provided.\r
1558**/\r
1559EFI_STATUS\r
1560EFIAPI\r
1561InternalCommandLineParse (\r
1562 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1563 OUT LIST_ENTRY **CheckPackage,\r
1564 OUT CHAR16 **ProblemParam OPTIONAL,\r
1565 IN BOOLEAN AutoPageBreak,\r
1566 IN CONST CHAR16 **Argv,\r
1567 IN UINTN Argc\r
1568 )\r
1569{\r
1570 UINTN LoopCounter;\r
1571 UINTN Count;\r
1572 ParamType CurrentItemType;\r
1573 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
1574 BOOLEAN GetItemValue;\r
1575\r
1576 CurrentItemPackage = NULL;\r
1577\r
1578 //\r
1579 // ASSERTs\r
1580 //\r
1581 ASSERT(CheckList != NULL);\r
1582 ASSERT(Argv != NULL);\r
1583\r
1584 Count = 0;\r
1585 GetItemValue = FALSE;\r
1586\r
1587 //\r
1588 // If there is only 1 item we dont need to do anything\r
1589 //\r
1590 if (Argc <= 1) {\r
1591 *CheckPackage = NULL;\r
1592 return (EFI_SUCCESS);\r
1593 }\r
1594\r
1595 //\r
1596 // initialize the linked list\r
1597 //\r
1598 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1599 InitializeListHead(*CheckPackage);\r
1600\r
1601 //\r
1602 // loop through each of the arguments\r
1603 //\r
1604 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1605 if (Argv[LoopCounter] == NULL) {\r
1606 //\r
1607 // do nothing for NULL argv\r
1608 //\r
d2b4564b 1609 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {\r
94b17fa1 1610 //\r
1611 // this is a flag\r
1612 //\r
1613 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1614 ASSERT(CurrentItemPackage != NULL);\r
1615 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r
1616 ASSERT(CurrentItemPackage->Name != NULL);\r
1617 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1618 CurrentItemPackage->Type = CurrentItemType;\r
1619 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 1620 CurrentItemPackage->Value = NULL;\r
94b17fa1 1621\r
1622 //\r
1623 // Does this flag require a value\r
1624 //\r
1625 if (CurrentItemPackage->Type == TypeValue) {\r
1626 //\r
1627 // trigger the next loop to populate the value of this item\r
1628 //\r
1629 GetItemValue = TRUE; \r
1630 } else {\r
1631 //\r
1632 // this item has no value expected; we are done\r
1633 //\r
9b3bf083 1634 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
94b17fa1 1635 }\r
b1f95a06 1636 } else if (GetItemValue == TRUE && InternalIsFlag(Argv[LoopCounter]) == FALSE) {\r
1637 ASSERT(CurrentItemPackage != NULL);\r
1638 //\r
1639 // get the item VALUE for the previous flag\r
1640 //\r
1641 GetItemValue = FALSE;\r
1642 CurrentItemPackage->Value = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
1643 ASSERT(CurrentItemPackage->Value != NULL);\r
1644 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
9b3bf083 1645 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
b1f95a06 1646 } else if (InternalIsFlag(Argv[LoopCounter]) == FALSE) {\r
1647 //\r
1648 // add this one as a non-flag\r
1649 //\r
1650 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1651 ASSERT(CurrentItemPackage != NULL);\r
1652 CurrentItemPackage->Name = NULL;\r
1653 CurrentItemPackage->Type = TypePosition;\r
1654 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r
1655 ASSERT(CurrentItemPackage->Value != NULL);\r
1656 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1657 CurrentItemPackage->OriginalPosition = Count++;\r
9b3bf083 1658 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
94b17fa1 1659 } else if (ProblemParam) {\r
1660 //\r
1661 // this was a non-recognised flag... error!\r
1662 //\r
d2b4564b 1663 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r
1664 ASSERT(*ProblemParam != NULL);\r
1665 StrCpy(*ProblemParam, Argv[LoopCounter]);\r
94b17fa1 1666 ShellCommandLineFreeVarList(*CheckPackage);\r
1667 *CheckPackage = NULL;\r
1668 return (EFI_VOLUME_CORRUPTED);\r
1669 } else {\r
1670 ShellCommandLineFreeVarList(*CheckPackage);\r
1671 *CheckPackage = NULL;\r
1672 return (EFI_VOLUME_CORRUPTED);\r
1673 }\r
1674 }\r
1675 //\r
1676 // support for AutoPageBreak\r
1677 //\r
1678 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
1679 ShellSetPageBreakMode(TRUE);\r
1680 }\r
1681 return (EFI_SUCCESS);\r
1682}\r
1683\r
1684/**\r
1685 Checks the command line arguments passed against the list of valid ones. \r
1686 Optionally removes NULL values first.\r
1687 \r
1688 If no initialization is required, then return RETURN_SUCCESS.\r
1689 \r
1690 @param CheckList pointer to list of parameters to check\r
1691 @param CheckPackage pointer to pointer to list checked values\r
1692 @param ProblemParam optional pointer to pointer to unicode string for \r
1693 the paramater that caused failure.\r
1694 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1695\r
1696 @retval EFI_SUCCESS The operation completed sucessfully.\r
1697 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1698 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1699 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1700 duplicated. the duplicated command line argument \r
1701 was returned in ProblemParam if provided.\r
1702 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r
1703 of the command line arguments was returned in \r
1704 ProblemParam if provided.\r
1705 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1706 the invalid command line argument was returned in\r
1707 ProblemParam if provided.\r
1708**/\r
1709EFI_STATUS\r
1710EFIAPI\r
1711ShellCommandLineParse (\r
1712 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1713 OUT LIST_ENTRY **CheckPackage,\r
1714 OUT CHAR16 **ProblemParam OPTIONAL,\r
1715 IN BOOLEAN AutoPageBreak\r
1716 )\r
1717{\r
1718 // \r
1719 // ASSERT that CheckList and CheckPackage aren't NULL\r
1720 //\r
1721 ASSERT(CheckList != NULL);\r
1722 ASSERT(CheckPackage != NULL);\r
1723\r
1724 // \r
1725 // Check for UEFI Shell 2.0 protocols\r
1726 //\r
1727 if (mEfiShellParametersProtocol != NULL) {\r
1728 return (InternalCommandLineParse(CheckList, \r
1729 CheckPackage, \r
1730 ProblemParam, \r
1731 AutoPageBreak, \r
08d7f8e8 1732 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
94b17fa1 1733 mEfiShellParametersProtocol->Argc ));\r
1734 }\r
1735\r
1736 // \r
1737 // ASSERT That EFI Shell is not required\r
1738 //\r
1739 ASSERT (mEfiShellInterface != NULL);\r
1740 return (InternalCommandLineParse(CheckList, \r
1741 CheckPackage, \r
1742 ProblemParam, \r
1743 AutoPageBreak, \r
08d7f8e8 1744 (CONST CHAR16**) mEfiShellInterface->Argv,\r
94b17fa1 1745 mEfiShellInterface->Argc ));\r
1746}\r
1747\r
1748/**\r
1749 Frees shell variable list that was returned from ShellCommandLineParse.\r
1750\r
1751 This function will free all the memory that was used for the CheckPackage\r
1752 list of postprocessed shell arguments.\r
1753\r
1754 this function has no return value.\r
1755\r
1756 if CheckPackage is NULL, then return\r
1757\r
1758 @param CheckPackage the list to de-allocate\r
1759 **/\r
1760VOID\r
1761EFIAPI\r
1762ShellCommandLineFreeVarList (\r
1763 IN LIST_ENTRY *CheckPackage\r
1764 )\r
1765{\r
1766 LIST_ENTRY *Node;\r
1767\r
1768 //\r
1769 // check for CheckPackage == NULL\r
1770 //\r
1771 if (CheckPackage == NULL) {\r
1772 return;\r
1773 }\r
1774\r
1775 //\r
1776 // for each node in the list\r
1777 //\r
9eb53ac3 1778 for ( Node = GetFirstNode(CheckPackage)\r
1779 ; Node != CheckPackage \r
1780 ; Node = GetFirstNode(CheckPackage)\r
1781 ){\r
94b17fa1 1782 //\r
1783 // Remove it from the list\r
1784 //\r
1785 RemoveEntryList(Node);\r
1786\r
1787 //\r
1788 // if it has a name free the name\r
1789 //\r
1790 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
1791 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
1792 }\r
1793\r
1794 //\r
1795 // if it has a value free the value\r
1796 //\r
1797 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
1798 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
1799 }\r
1800 \r
1801 //\r
1802 // free the node structure\r
1803 //\r
1804 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
1805 }\r
1806 //\r
1807 // free the list head node\r
1808 //\r
1809 FreePool(CheckPackage);\r
1810}\r
1811/**\r
1812 Checks for presence of a flag parameter\r
1813\r
1814 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
1815\r
1816 if CheckPackage is NULL then return FALSE.\r
1817 if KeyString is NULL then ASSERT()\r
1818 \r
1819 @param CheckPackage The package of parsed command line arguments\r
1820 @param KeyString the Key of the command line argument to check for\r
1821\r
1822 @retval TRUE the flag is on the command line\r
1823 @retval FALSE the flag is not on the command line\r
1824 **/\r
1825BOOLEAN\r
1826EFIAPI\r
1827ShellCommandLineGetFlag (\r
1828 IN CONST LIST_ENTRY *CheckPackage,\r
1829 IN CHAR16 *KeyString\r
1830 )\r
1831{\r
1832 LIST_ENTRY *Node;\r
1833\r
1834 //\r
1835 // ASSERT that both CheckPackage and KeyString aren't NULL\r
1836 //\r
1837 ASSERT(KeyString != NULL);\r
1838\r
1839 //\r
1840 // return FALSE for no package\r
1841 //\r
1842 if (CheckPackage == NULL) {\r
1843 return (FALSE);\r
1844 }\r
1845\r
1846 //\r
1847 // enumerate through the list of parametrs\r
1848 //\r
9eb53ac3 1849 for ( Node = GetFirstNode(CheckPackage) \r
1850 ; !IsNull (CheckPackage, Node) \r
1851 ; Node = GetNextNode(CheckPackage, Node) \r
1852 ){\r
94b17fa1 1853 //\r
1854 // If the Name matches, return TRUE (and there may be NULL name)\r
1855 //\r
1856 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 1857 //\r
1858 // If Type is TypeStart then only compare the begining of the strings\r
1859 //\r
1860 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
1861 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
1862 ){\r
1863 return (TRUE);\r
1864 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 1865 return (TRUE);\r
1866 }\r
1867 }\r
1868 }\r
1869 return (FALSE);\r
1870}\r
1871/**\r
1872 returns value from command line argument\r
1873\r
1874 value parameters are in the form of "-<Key> value" or "/<Key> value"\r
1875 \r
1876 if CheckPackage is NULL, then return NULL;\r
1877\r
1878 @param CheckPackage The package of parsed command line arguments\r
1879 @param KeyString the Key of the command line argument to check for\r
1880\r
1881 @retval NULL the flag is not on the command line\r
1882 @return !=NULL pointer to unicode string of the value\r
1883 **/\r
1884CONST CHAR16*\r
1885EFIAPI\r
1886ShellCommandLineGetValue (\r
1887 IN CONST LIST_ENTRY *CheckPackage,\r
1888 IN CHAR16 *KeyString\r
1889 )\r
1890{\r
1891 LIST_ENTRY *Node;\r
1892\r
1893 //\r
1894 // check for CheckPackage == NULL\r
1895 //\r
1896 if (CheckPackage == NULL) {\r
1897 return (NULL);\r
1898 }\r
1899\r
1900 //\r
1901 // enumerate through the list of parametrs\r
1902 //\r
9eb53ac3 1903 for ( Node = GetFirstNode(CheckPackage) \r
1904 ; !IsNull (CheckPackage, Node) \r
1905 ; Node = GetNextNode(CheckPackage, Node) \r
1906 ){\r
94b17fa1 1907 //\r
1908 // If the Name matches, return the value (name can be NULL)\r
1909 //\r
1910 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 1911 //\r
1912 // If Type is TypeStart then only compare the begining of the strings\r
1913 //\r
1914 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
1915 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
1916 ){\r
1917 //\r
1918 // return the string part after the flag\r
1919 //\r
1920 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
1921 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
1922 //\r
1923 // return the value\r
1924 //\r
94b17fa1 1925 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
1926 }\r
1927 }\r
1928 }\r
1929 return (NULL);\r
1930}\r
1931/**\r
1932 returns raw value from command line argument\r
1933\r
1934 raw value parameters are in the form of "value" in a specific position in the list\r
1935 \r
1936 if CheckPackage is NULL, then return NULL;\r
1937\r
1938 @param CheckPackage The package of parsed command line arguments\r
1939 @param Position the position of the value \r
1940\r
1941 @retval NULL the flag is not on the command line\r
1942 @return !=NULL pointer to unicode string of the value\r
1943 **/\r
1944CONST CHAR16*\r
1945EFIAPI\r
1946ShellCommandLineGetRawValue (\r
1947 IN CONST LIST_ENTRY *CheckPackage,\r
1948 IN UINT32 Position\r
1949 )\r
1950{\r
1951 LIST_ENTRY *Node;\r
1952\r
1953 //\r
1954 // check for CheckPackage == NULL\r
1955 //\r
1956 if (CheckPackage == NULL) {\r
1957 return (NULL);\r
1958 }\r
1959\r
1960 //\r
1961 // enumerate through the list of parametrs\r
1962 //\r
b82bfcc1 1963 for ( Node = GetFirstNode(CheckPackage) \r
1964 ; !IsNull (CheckPackage, Node) \r
1965 ; Node = GetNextNode(CheckPackage, Node) \r
1966 ){\r
94b17fa1 1967 //\r
1968 // If the position matches, return the value\r
1969 //\r
1970 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
1971 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
1972 }\r
1973 }\r
1974 return (NULL);\r
b1f95a06 1975}\r
975136ab 1976/**\r
1977 This is a find and replace function. it will return the NewString as a copy of \r
1978 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
1979\r
1980 If the string would grow bigger than NewSize it will halt and return error.\r
1981\r
1982 @param[in] SourceString String with source buffer\r
b82bfcc1 1983 @param[in,out] NewString String with resultant buffer\r
975136ab 1984 @param[in] NewSize Size in bytes of NewString\r
1985 @param[in] FindTarget String to look for\r
1986 @param[in[ ReplaceWith String to replace FindTarget with\r
1987\r
1988 @retval EFI_INVALID_PARAMETER SourceString was NULL\r
1989 @retval EFI_INVALID_PARAMETER NewString was NULL\r
1990 @retval EFI_INVALID_PARAMETER FindTarget was NULL\r
1991 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL\r
1992 @retval EFI_INVALID_PARAMETER FindTarget had length < 1\r
1993 @retval EFI_INVALID_PARAMETER SourceString had length < 1\r
1994 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold \r
1995 the new string (truncation occurred)\r
1996 @retval EFI_SUCCESS the string was sucessfully copied with replacement\r
1997**/\r
1998\r
1999EFI_STATUS\r
2000EFIAPI\r
2001CopyReplace(\r
2002 IN CHAR16 CONST *SourceString,\r
2003 IN CHAR16 *NewString,\r
2004 IN UINTN NewSize,\r
2005 IN CONST CHAR16 *FindTarget,\r
2006 IN CONST CHAR16 *ReplaceWith\r
2007 ){\r
0158294b 2008 UINTN Size;\r
975136ab 2009 if ( (SourceString == NULL)\r
2010 || (NewString == NULL)\r
2011 || (FindTarget == NULL)\r
2012 || (ReplaceWith == NULL)\r
2013 || (StrLen(FindTarget) < 1)\r
2014 || (StrLen(SourceString) < 1)\r
2015 ){\r
2016 return (EFI_INVALID_PARAMETER);\r
2017 }\r
2018 NewString = SetMem16(NewString, NewSize, L'\0');\r
2019 while (*SourceString != L'\0') {\r
2020 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {\r
2021 SourceString += StrLen(FindTarget);\r
0158294b 2022 Size = StrSize(NewString);\r
2023 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r
975136ab 2024 return (EFI_BUFFER_TOO_SMALL);\r
2025 }\r
2026 StrCat(NewString, ReplaceWith);\r
2027 } else {\r
0158294b 2028 Size = StrSize(NewString);\r
2029 if (Size + sizeof(CHAR16) > NewSize) {\r
975136ab 2030 return (EFI_BUFFER_TOO_SMALL);\r
2031 }\r
2032 StrnCat(NewString, SourceString, 1);\r
2033 SourceString++;\r
2034 }\r
2035 }\r
2036 return (EFI_SUCCESS);\r
2037}\r
b1f95a06 2038\r
2039/**\r
2040 Print at a specific location on the screen.\r
2041\r
f1b87e7a 2042 This function will move the cursor to a given screen location and print the specified string\r
b1f95a06 2043 \r
2044 If -1 is specified for either the Row or Col the current screen location for BOTH \r
f1b87e7a 2045 will be used.\r
b1f95a06 2046\r
2047 if either Row or Col is out of range for the current console, then ASSERT\r
2048 if Format is NULL, then ASSERT\r
2049\r
2050 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2051 the following additional flags:\r
2052 %N - Set output attribute to normal\r
2053 %H - Set output attribute to highlight\r
2054 %E - Set output attribute to error\r
2055 %B - Set output attribute to blue color\r
2056 %V - Set output attribute to green color\r
2057\r
2058 Note: The background color is controlled by the shell command cls.\r
2059\r
2060 @param[in] Row the row to print at\r
2061 @param[in] Col the column to print at\r
2062 @param[in] Format the format string\r
2063\r
2064 @return the number of characters printed to the screen\r
2065**/\r
2066\r
2067UINTN\r
2068EFIAPI\r
2069ShellPrintEx(\r
2070 IN INT32 Col OPTIONAL,\r
2071 IN INT32 Row OPTIONAL,\r
2072 IN CONST CHAR16 *Format,\r
2073 ...\r
2074 ){\r
2075 VA_LIST Marker;\r
2076 UINTN BufferSize;\r
975136ab 2077 CHAR16 *PostReplaceFormat;\r
2078 CHAR16 *PostReplaceFormat2;\r
b1f95a06 2079 UINTN Return;\r
f1b87e7a 2080\r
b1f95a06 2081 EFI_STATUS Status;\r
975136ab 2082 UINTN NormalAttribute;\r
2083 CHAR16 *ResumeLocation;\r
2084 CHAR16 *FormatWalker;\r
2085\r
2086 VA_START (Marker, Format);\r
b1f95a06 2087 \r
2088 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
975136ab 2089 PostReplaceFormat = AllocateZeroPool (BufferSize);\r
2090 ASSERT (PostReplaceFormat != NULL);\r
2091 PostReplaceFormat2 = AllocateZeroPool (BufferSize);\r
2092 ASSERT (PostReplaceFormat2 != NULL);\r
b1f95a06 2093\r
975136ab 2094 //\r
2095 // Back and forth each time fixing up 1 of our flags...\r
2096 //\r
2097 Status = CopyReplace(Format, PostReplaceFormat, BufferSize, L"%N", L"%%N");\r
2098 ASSERT_EFI_ERROR(Status);\r
2099 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%E", L"%%E");\r
2100 ASSERT_EFI_ERROR(Status);\r
2101 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%H", L"%%H");\r
2102 ASSERT_EFI_ERROR(Status);\r
2103 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%B", L"%%B");\r
2104 ASSERT_EFI_ERROR(Status);\r
2105 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%V", L"%%V");\r
2106 ASSERT_EFI_ERROR(Status);\r
2107\r
2108 //\r
2109 // Use the last buffer from replacing to print from...\r
2110 //\r
2111 Return = UnicodeVSPrint (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);\r
2112\r
2113 FreePool(PostReplaceFormat);\r
b1f95a06 2114\r
2115 if (Col != -1 && Row != -1) {\r
b1f95a06 2116 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2117 ASSERT_EFI_ERROR(Status);\r
975136ab 2118 }\r
2119\r
2120 NormalAttribute = gST->ConOut->Mode->Attribute;\r
2121 FormatWalker = PostReplaceFormat2;\r
2122 while (*FormatWalker != L'\0') {\r
2123 //\r
2124 // Find the next attribute change request\r
2125 //\r
2126 ResumeLocation = StrStr(FormatWalker, L"%");\r
2127 if (ResumeLocation != NULL) {\r
2128 *ResumeLocation = L'\0';\r
2129 }\r
2130 //\r
2131 // print the current FormatWalker string\r
2132 //\r
2133 Status = gST->ConOut->OutputString(gST->ConOut, FormatWalker);\r
b1f95a06 2134 ASSERT_EFI_ERROR(Status);\r
975136ab 2135 //\r
2136 // update the attribute\r
2137 //\r
2138 if (ResumeLocation != NULL) {\r
2139 switch (*(ResumeLocation+1)) {\r
2140 case (L'N'):\r
2141 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2142 break;\r
2143 case (L'E'):\r
2144 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2145 break;\r
2146 case (L'H'):\r
2147 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2148 break;\r
2149 case (L'B'):\r
2150 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2151 break;\r
2152 case (L'V'):\r
2153 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2154 break;\r
2155 default:\r
2156 ASSERT(FALSE);\r
2157 break;\r
2158 }\r
2159 } else {\r
2160 //\r
2161 // reset to normal now...\r
2162 //\r
2163 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2164 break;\r
2165 }\r
2166\r
2167 //\r
2168 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2169 //\r
2170 FormatWalker = ResumeLocation + 2;\r
2171 }\r
b1f95a06 2172\r
975136ab 2173 FreePool(PostReplaceFormat2);\r
2174\r
b1f95a06 2175 return (Return);\r
5f7431d0 2176}\r