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