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