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