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