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