]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLib/UefiShellLib.c
fix build breaks. and allow for new lists to be created.
[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. If
1306 *ListHead is NULL then it must be callee freed.
1307
1308 @param Arg pointer to path string
1309 @param OpenMode mode to open files with
1310 @param ListHead head of linked list of results
1311
1312 @retval EFI_SUCCESS the operation was sucessful and the list head
1313 contains the list of opened files
1314 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1315 *ListHead is set to NULL.
1316 @return != EFI_SUCCESS the operation failed
1317
1318 @sa InternalShellConvertFileListType
1319 **/
1320 EFI_STATUS
1321 EFIAPI
1322 ShellOpenFileMetaArg (
1323 IN CHAR16 *Arg,
1324 IN UINT64 OpenMode,
1325 IN OUT EFI_SHELL_FILE_INFO **ListHead
1326 )
1327 {
1328 EFI_STATUS Status;
1329 LIST_ENTRY mOldStyleFileList;
1330
1331 //
1332 // ASSERT that Arg and ListHead are not NULL
1333 //
1334 ASSERT(Arg != NULL);
1335 ASSERT(ListHead != NULL);
1336
1337 //
1338 // Check for UEFI Shell 2.0 protocols
1339 //
1340 if (mEfiShellProtocol != NULL) {
1341 if (*ListHead == NULL) {
1342 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1343 if (*ListHead == NULL) {
1344 return (EFI_OUT_OF_RESOURCES);
1345 }
1346 InitializeListHead(&((*ListHead)->Link));
1347 }
1348 return (mEfiShellProtocol->OpenFileList(Arg,
1349 OpenMode,
1350 ListHead));
1351 }
1352
1353 //
1354 // ASSERT that we must have EFI shell
1355 //
1356 ASSERT(mEfiShellEnvironment2 != NULL);
1357
1358 //
1359 // make sure the list head is initialized
1360 //
1361 InitializeListHead(&mOldStyleFileList);
1362
1363 //
1364 // Get the EFI Shell list of files
1365 //
1366 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
1367 if (EFI_ERROR(Status)) {
1368 *ListHead = NULL;
1369 return (Status);
1370 }
1371
1372 if (*ListHead == NULL) {
1373 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1374 if (*ListHead == NULL) {
1375 return (EFI_OUT_OF_RESOURCES);
1376 }
1377 }
1378
1379 //
1380 // Convert that to equivalent of UEFI Shell 2.0 structure
1381 //
1382 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1383
1384 //
1385 // Free the EFI Shell version that was converted.
1386 //
1387 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1388
1389 return (Status);
1390 }
1391 /**
1392 Free the linked list returned from ShellOpenFileMetaArg
1393
1394 if ListHead is NULL then ASSERT()
1395
1396 @param ListHead the pointer to free
1397
1398 @retval EFI_SUCCESS the operation was sucessful
1399 **/
1400 EFI_STATUS
1401 EFIAPI
1402 ShellCloseFileMetaArg (
1403 IN OUT EFI_SHELL_FILE_INFO **ListHead
1404 )
1405 {
1406 LIST_ENTRY *Node;
1407
1408 //
1409 // ASSERT that ListHead is not NULL
1410 //
1411 ASSERT(ListHead != NULL);
1412
1413 //
1414 // Check for UEFI Shell 2.0 protocols
1415 //
1416 if (mEfiShellProtocol != NULL) {
1417 return (mEfiShellProtocol->FreeFileList(ListHead));
1418 } else {
1419 //
1420 // Since this is EFI Shell version we need to free our internally made copy
1421 // of the list
1422 //
1423 for ( Node = GetFirstNode(&(*ListHead)->Link)
1424 ; IsListEmpty(&(*ListHead)->Link) == FALSE
1425 ; Node = GetFirstNode(&(*ListHead)->Link)) {
1426 RemoveEntryList(Node);
1427 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
1428 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1429 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1430 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1431 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1432 }
1433 return EFI_SUCCESS;
1434 }
1435 }
1436
1437 typedef struct {
1438 LIST_ENTRY Link;
1439 CHAR16 *Name;
1440 ParamType Type;
1441 CHAR16 *Value;
1442 UINTN OriginalPosition;
1443 } SHELL_PARAM_PACKAGE;
1444
1445 /**
1446 Checks the list of valid arguments and returns TRUE if the item was found. If the
1447 return value is TRUE then the type parameter is set also.
1448
1449 if CheckList is NULL then ASSERT();
1450 if Name is NULL then ASSERT();
1451 if Type is NULL then ASSERT();
1452
1453 @param Type pointer to type of parameter if it was found
1454 @param Name pointer to Name of parameter found
1455 @param CheckList List to check against
1456
1457 @retval TRUE the Parameter was found. Type is valid.
1458 @retval FALSE the Parameter was not found. Type is not valid.
1459 **/
1460 BOOLEAN
1461 EFIAPI
1462 InternalIsOnCheckList (
1463 IN CONST CHAR16 *Name,
1464 IN CONST SHELL_PARAM_ITEM *CheckList,
1465 OUT ParamType *Type
1466 )
1467 {
1468 SHELL_PARAM_ITEM *TempListItem;
1469
1470 //
1471 // ASSERT that all 3 pointer parameters aren't NULL
1472 //
1473 ASSERT(CheckList != NULL);
1474 ASSERT(Type != NULL);
1475 ASSERT(Name != NULL);
1476
1477 //
1478 // question mark and page break mode are always supported
1479 //
1480 if ((StrCmp(Name, L"-?") == 0) ||
1481 (StrCmp(Name, L"-b") == 0)
1482 ) {
1483 return (TRUE);
1484 }
1485
1486 //
1487 // Enumerate through the list
1488 //
1489 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1490 //
1491 // If the Type is TypeStart only check the first characters of the passed in param
1492 // If it matches set the type and return TRUE
1493 //
1494 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1495 *Type = TempListItem->Type;
1496 return (TRUE);
1497 } else if (StrCmp(Name, TempListItem->Name) == 0) {
1498 *Type = TempListItem->Type;
1499 return (TRUE);
1500 }
1501 }
1502 return (FALSE);
1503 }
1504 /**
1505 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1506
1507 @param Name pointer to Name of parameter found
1508
1509 @retval TRUE the Parameter is a flag.
1510 @retval FALSE the Parameter not a flag
1511 **/
1512 BOOLEAN
1513 EFIAPI
1514 InternalIsFlag (
1515 IN CONST CHAR16 *Name
1516 )
1517 {
1518 //
1519 // ASSERT that Name isn't NULL
1520 //
1521 ASSERT(Name != NULL);
1522
1523 //
1524 // If the Name has a / or - as the first character return TRUE
1525 //
1526 if ((Name[0] == L'/') ||
1527 (Name[0] == L'-') ||
1528 (Name[0] == L'+')
1529 ) {
1530 return (TRUE);
1531 }
1532 return (FALSE);
1533 }
1534
1535 /**
1536 Checks the command line arguments passed against the list of valid ones.
1537
1538 If no initialization is required, then return RETURN_SUCCESS.
1539
1540 @param CheckList pointer to list of parameters to check
1541 @param CheckPackage pointer to pointer to list checked values
1542 @param ProblemParam optional pointer to pointer to unicode string for
1543 the paramater that caused failure. If used then the
1544 caller is responsible for freeing the memory.
1545 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1546 @param Argc Count of parameters in Argv
1547 @param Argv pointer to array of parameters
1548
1549 @retval EFI_SUCCESS The operation completed sucessfully.
1550 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1551 @retval EFI_INVALID_PARAMETER A parameter was invalid
1552 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1553 duplicated. the duplicated command line argument
1554 was returned in ProblemParam if provided.
1555 @retval EFI_NOT_FOUND a argument required a value that was missing.
1556 the invalid command line argument was returned in
1557 ProblemParam if provided.
1558 **/
1559 EFI_STATUS
1560 EFIAPI
1561 InternalCommandLineParse (
1562 IN CONST SHELL_PARAM_ITEM *CheckList,
1563 OUT LIST_ENTRY **CheckPackage,
1564 OUT CHAR16 **ProblemParam OPTIONAL,
1565 IN BOOLEAN AutoPageBreak,
1566 IN CONST CHAR16 **Argv,
1567 IN UINTN Argc
1568 )
1569 {
1570 UINTN LoopCounter;
1571 UINTN Count;
1572 ParamType CurrentItemType;
1573 SHELL_PARAM_PACKAGE *CurrentItemPackage;
1574 BOOLEAN GetItemValue;
1575
1576 CurrentItemPackage = NULL;
1577
1578 //
1579 // ASSERTs
1580 //
1581 ASSERT(CheckList != NULL);
1582 ASSERT(Argv != NULL);
1583
1584 Count = 0;
1585 GetItemValue = FALSE;
1586
1587 //
1588 // If there is only 1 item we dont need to do anything
1589 //
1590 if (Argc <= 1) {
1591 *CheckPackage = NULL;
1592 return (EFI_SUCCESS);
1593 }
1594
1595 //
1596 // initialize the linked list
1597 //
1598 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1599 InitializeListHead(*CheckPackage);
1600
1601 //
1602 // loop through each of the arguments
1603 //
1604 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1605 if (Argv[LoopCounter] == NULL) {
1606 //
1607 // do nothing for NULL argv
1608 //
1609 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {
1610 //
1611 // this is a flag
1612 //
1613 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1614 ASSERT(CurrentItemPackage != NULL);
1615 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1616 ASSERT(CurrentItemPackage->Name != NULL);
1617 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1618 CurrentItemPackage->Type = CurrentItemType;
1619 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
1620 CurrentItemPackage->Value = NULL;
1621
1622 //
1623 // Does this flag require a value
1624 //
1625 if (CurrentItemPackage->Type == TypeValue) {
1626 //
1627 // trigger the next loop to populate the value of this item
1628 //
1629 GetItemValue = TRUE;
1630 } else {
1631 //
1632 // this item has no value expected; we are done
1633 //
1634 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1635 }
1636 } else if (GetItemValue == TRUE && InternalIsFlag(Argv[LoopCounter]) == FALSE) {
1637 ASSERT(CurrentItemPackage != NULL);
1638 //
1639 // get the item VALUE for the previous flag
1640 //
1641 GetItemValue = FALSE;
1642 CurrentItemPackage->Value = AllocateZeroPool(StrSize(Argv[LoopCounter]));
1643 ASSERT(CurrentItemPackage->Value != NULL);
1644 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1645 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1646 } else if (InternalIsFlag(Argv[LoopCounter]) == FALSE) {
1647 //
1648 // add this one as a non-flag
1649 //
1650 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1651 ASSERT(CurrentItemPackage != NULL);
1652 CurrentItemPackage->Name = NULL;
1653 CurrentItemPackage->Type = TypePosition;
1654 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1655 ASSERT(CurrentItemPackage->Value != NULL);
1656 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1657 CurrentItemPackage->OriginalPosition = Count++;
1658 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1659 } else if (ProblemParam) {
1660 //
1661 // this was a non-recognised flag... error!
1662 //
1663 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
1664 ASSERT(*ProblemParam != NULL);
1665 StrCpy(*ProblemParam, Argv[LoopCounter]);
1666 ShellCommandLineFreeVarList(*CheckPackage);
1667 *CheckPackage = NULL;
1668 return (EFI_VOLUME_CORRUPTED);
1669 } else {
1670 ShellCommandLineFreeVarList(*CheckPackage);
1671 *CheckPackage = NULL;
1672 return (EFI_VOLUME_CORRUPTED);
1673 }
1674 }
1675 //
1676 // support for AutoPageBreak
1677 //
1678 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
1679 ShellSetPageBreakMode(TRUE);
1680 }
1681 return (EFI_SUCCESS);
1682 }
1683
1684 /**
1685 Checks the command line arguments passed against the list of valid ones.
1686 Optionally removes NULL values first.
1687
1688 If no initialization is required, then return RETURN_SUCCESS.
1689
1690 @param CheckList pointer to list of parameters to check
1691 @param CheckPackage pointer to pointer to list checked values
1692 @param ProblemParam optional pointer to pointer to unicode string for
1693 the paramater that caused failure.
1694 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1695
1696 @retval EFI_SUCCESS The operation completed sucessfully.
1697 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1698 @retval EFI_INVALID_PARAMETER A parameter was invalid
1699 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1700 duplicated. the duplicated command line argument
1701 was returned in ProblemParam if provided.
1702 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1703 of the command line arguments was returned in
1704 ProblemParam if provided.
1705 @retval EFI_NOT_FOUND a argument required a value that was missing.
1706 the invalid command line argument was returned in
1707 ProblemParam if provided.
1708 **/
1709 EFI_STATUS
1710 EFIAPI
1711 ShellCommandLineParse (
1712 IN CONST SHELL_PARAM_ITEM *CheckList,
1713 OUT LIST_ENTRY **CheckPackage,
1714 OUT CHAR16 **ProblemParam OPTIONAL,
1715 IN BOOLEAN AutoPageBreak
1716 )
1717 {
1718 //
1719 // ASSERT that CheckList and CheckPackage aren't NULL
1720 //
1721 ASSERT(CheckList != NULL);
1722 ASSERT(CheckPackage != NULL);
1723
1724 //
1725 // Check for UEFI Shell 2.0 protocols
1726 //
1727 if (mEfiShellParametersProtocol != NULL) {
1728 return (InternalCommandLineParse(CheckList,
1729 CheckPackage,
1730 ProblemParam,
1731 AutoPageBreak,
1732 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
1733 mEfiShellParametersProtocol->Argc ));
1734 }
1735
1736 //
1737 // ASSERT That EFI Shell is not required
1738 //
1739 ASSERT (mEfiShellInterface != NULL);
1740 return (InternalCommandLineParse(CheckList,
1741 CheckPackage,
1742 ProblemParam,
1743 AutoPageBreak,
1744 (CONST CHAR16**) mEfiShellInterface->Argv,
1745 mEfiShellInterface->Argc ));
1746 }
1747
1748 /**
1749 Frees shell variable list that was returned from ShellCommandLineParse.
1750
1751 This function will free all the memory that was used for the CheckPackage
1752 list of postprocessed shell arguments.
1753
1754 this function has no return value.
1755
1756 if CheckPackage is NULL, then return
1757
1758 @param CheckPackage the list to de-allocate
1759 **/
1760 VOID
1761 EFIAPI
1762 ShellCommandLineFreeVarList (
1763 IN LIST_ENTRY *CheckPackage
1764 )
1765 {
1766 LIST_ENTRY *Node;
1767
1768 //
1769 // check for CheckPackage == NULL
1770 //
1771 if (CheckPackage == NULL) {
1772 return;
1773 }
1774
1775 //
1776 // for each node in the list
1777 //
1778 for ( Node = GetFirstNode(CheckPackage)
1779 ; Node != CheckPackage
1780 ; Node = GetFirstNode(CheckPackage)
1781 ){
1782 //
1783 // Remove it from the list
1784 //
1785 RemoveEntryList(Node);
1786
1787 //
1788 // if it has a name free the name
1789 //
1790 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1791 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
1792 }
1793
1794 //
1795 // if it has a value free the value
1796 //
1797 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
1798 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
1799 }
1800
1801 //
1802 // free the node structure
1803 //
1804 FreePool((SHELL_PARAM_PACKAGE*)Node);
1805 }
1806 //
1807 // free the list head node
1808 //
1809 FreePool(CheckPackage);
1810 }
1811 /**
1812 Checks for presence of a flag parameter
1813
1814 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1815
1816 if CheckPackage is NULL then return FALSE.
1817 if KeyString is NULL then ASSERT()
1818
1819 @param CheckPackage The package of parsed command line arguments
1820 @param KeyString the Key of the command line argument to check for
1821
1822 @retval TRUE the flag is on the command line
1823 @retval FALSE the flag is not on the command line
1824 **/
1825 BOOLEAN
1826 EFIAPI
1827 ShellCommandLineGetFlag (
1828 IN CONST LIST_ENTRY *CheckPackage,
1829 IN CHAR16 *KeyString
1830 )
1831 {
1832 LIST_ENTRY *Node;
1833
1834 //
1835 // ASSERT that both CheckPackage and KeyString aren't NULL
1836 //
1837 ASSERT(KeyString != NULL);
1838
1839 //
1840 // return FALSE for no package
1841 //
1842 if (CheckPackage == NULL) {
1843 return (FALSE);
1844 }
1845
1846 //
1847 // enumerate through the list of parametrs
1848 //
1849 for ( Node = GetFirstNode(CheckPackage)
1850 ; !IsNull (CheckPackage, Node)
1851 ; Node = GetNextNode(CheckPackage, Node)
1852 ){
1853 //
1854 // If the Name matches, return TRUE (and there may be NULL name)
1855 //
1856 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1857 //
1858 // If Type is TypeStart then only compare the begining of the strings
1859 //
1860 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
1861 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
1862 ){
1863 return (TRUE);
1864 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
1865 return (TRUE);
1866 }
1867 }
1868 }
1869 return (FALSE);
1870 }
1871 /**
1872 returns value from command line argument
1873
1874 value parameters are in the form of "-<Key> value" or "/<Key> value"
1875
1876 if CheckPackage is NULL, then return NULL;
1877
1878 @param CheckPackage The package of parsed command line arguments
1879 @param KeyString the Key of the command line argument to check for
1880
1881 @retval NULL the flag is not on the command line
1882 @return !=NULL pointer to unicode string of the value
1883 **/
1884 CONST CHAR16*
1885 EFIAPI
1886 ShellCommandLineGetValue (
1887 IN CONST LIST_ENTRY *CheckPackage,
1888 IN CHAR16 *KeyString
1889 )
1890 {
1891 LIST_ENTRY *Node;
1892
1893 //
1894 // check for CheckPackage == NULL
1895 //
1896 if (CheckPackage == NULL) {
1897 return (NULL);
1898 }
1899
1900 //
1901 // enumerate through the list of parametrs
1902 //
1903 for ( Node = GetFirstNode(CheckPackage)
1904 ; !IsNull (CheckPackage, Node)
1905 ; Node = GetNextNode(CheckPackage, Node)
1906 ){
1907 //
1908 // If the Name matches, return the value (name can be NULL)
1909 //
1910 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1911 //
1912 // If Type is TypeStart then only compare the begining of the strings
1913 //
1914 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
1915 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
1916 ){
1917 //
1918 // return the string part after the flag
1919 //
1920 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
1921 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
1922 //
1923 // return the value
1924 //
1925 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
1926 }
1927 }
1928 }
1929 return (NULL);
1930 }
1931 /**
1932 returns raw value from command line argument
1933
1934 raw value parameters are in the form of "value" in a specific position in the list
1935
1936 if CheckPackage is NULL, then return NULL;
1937
1938 @param CheckPackage The package of parsed command line arguments
1939 @param Position the position of the value
1940
1941 @retval NULL the flag is not on the command line
1942 @return !=NULL pointer to unicode string of the value
1943 **/
1944 CONST CHAR16*
1945 EFIAPI
1946 ShellCommandLineGetRawValue (
1947 IN CONST LIST_ENTRY *CheckPackage,
1948 IN UINT32 Position
1949 )
1950 {
1951 LIST_ENTRY *Node;
1952
1953 //
1954 // check for CheckPackage == NULL
1955 //
1956 if (CheckPackage == NULL) {
1957 return (NULL);
1958 }
1959
1960 //
1961 // enumerate through the list of parametrs
1962 //
1963 for ( Node = GetFirstNode(CheckPackage)
1964 ; !IsNull (CheckPackage, Node)
1965 ; Node = GetNextNode(CheckPackage, Node)
1966 ){
1967 //
1968 // If the position matches, return the value
1969 //
1970 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
1971 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
1972 }
1973 }
1974 return (NULL);
1975 }
1976 /**
1977 This is a find and replace function. it will return the NewString as a copy of
1978 SourceString with each instance of FindTarget replaced with ReplaceWith.
1979
1980 If the string would grow bigger than NewSize it will halt and return error.
1981
1982 @param[in] SourceString String with source buffer
1983 @param[in,out] NewString String with resultant buffer
1984 @param[in] NewSize Size in bytes of NewString
1985 @param[in] FindTarget String to look for
1986 @param[in[ ReplaceWith String to replace FindTarget with
1987
1988 @retval EFI_INVALID_PARAMETER SourceString was NULL
1989 @retval EFI_INVALID_PARAMETER NewString was NULL
1990 @retval EFI_INVALID_PARAMETER FindTarget was NULL
1991 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
1992 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
1993 @retval EFI_INVALID_PARAMETER SourceString had length < 1
1994 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
1995 the new string (truncation occurred)
1996 @retval EFI_SUCCESS the string was sucessfully copied with replacement
1997 **/
1998
1999 EFI_STATUS
2000 EFIAPI
2001 CopyReplace(
2002 IN CHAR16 CONST *SourceString,
2003 IN CHAR16 *NewString,
2004 IN UINTN NewSize,
2005 IN CONST CHAR16 *FindTarget,
2006 IN CONST CHAR16 *ReplaceWith
2007 ){
2008 if ( (SourceString == NULL)
2009 || (NewString == NULL)
2010 || (FindTarget == NULL)
2011 || (ReplaceWith == NULL)
2012 || (StrLen(FindTarget) < 1)
2013 || (StrLen(SourceString) < 1)
2014 ){
2015 return (EFI_INVALID_PARAMETER);
2016 }
2017 NewString = SetMem16(NewString, NewSize, L'\0');
2018 while (*SourceString != L'\0') {
2019 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {
2020 SourceString += StrLen(FindTarget);
2021 if ((StrSize(NewString) + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {
2022 return (EFI_BUFFER_TOO_SMALL);
2023 }
2024 StrCat(NewString, ReplaceWith);
2025 } else {
2026 if (StrSize(NewString) + sizeof(CHAR16) > NewSize) {
2027 return (EFI_BUFFER_TOO_SMALL);
2028 }
2029 StrnCat(NewString, SourceString, 1);
2030 SourceString++;
2031 }
2032 }
2033 return (EFI_SUCCESS);
2034 }
2035
2036 /**
2037 Print at a specific location on the screen.
2038
2039 This function will move the cursor to a given screen location and print the specified string
2040
2041 If -1 is specified for either the Row or Col the current screen location for BOTH
2042 will be used.
2043
2044 if either Row or Col is out of range for the current console, then ASSERT
2045 if Format is NULL, then ASSERT
2046
2047 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2048 the following additional flags:
2049 %N - Set output attribute to normal
2050 %H - Set output attribute to highlight
2051 %E - Set output attribute to error
2052 %B - Set output attribute to blue color
2053 %V - Set output attribute to green color
2054
2055 Note: The background color is controlled by the shell command cls.
2056
2057 @param[in] Row the row to print at
2058 @param[in] Col the column to print at
2059 @param[in] Format the format string
2060
2061 @return the number of characters printed to the screen
2062 **/
2063
2064 UINTN
2065 EFIAPI
2066 ShellPrintEx(
2067 IN INT32 Col OPTIONAL,
2068 IN INT32 Row OPTIONAL,
2069 IN CONST CHAR16 *Format,
2070 ...
2071 ){
2072 VA_LIST Marker;
2073 UINTN BufferSize;
2074 CHAR16 *PostReplaceFormat;
2075 CHAR16 *PostReplaceFormat2;
2076 UINTN Return;
2077
2078 EFI_STATUS Status;
2079 UINTN NormalAttribute;
2080 CHAR16 *ResumeLocation;
2081 CHAR16 *FormatWalker;
2082
2083 VA_START (Marker, Format);
2084
2085 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
2086 PostReplaceFormat = AllocateZeroPool (BufferSize);
2087 ASSERT (PostReplaceFormat != NULL);
2088 PostReplaceFormat2 = AllocateZeroPool (BufferSize);
2089 ASSERT (PostReplaceFormat2 != NULL);
2090
2091 //
2092 // Back and forth each time fixing up 1 of our flags...
2093 //
2094 Status = CopyReplace(Format, PostReplaceFormat, BufferSize, L"%N", L"%%N");
2095 ASSERT_EFI_ERROR(Status);
2096 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%E", L"%%E");
2097 ASSERT_EFI_ERROR(Status);
2098 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%H", L"%%H");
2099 ASSERT_EFI_ERROR(Status);
2100 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%B", L"%%B");
2101 ASSERT_EFI_ERROR(Status);
2102 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%V", L"%%V");
2103 ASSERT_EFI_ERROR(Status);
2104
2105 //
2106 // Use the last buffer from replacing to print from...
2107 //
2108 Return = UnicodeVSPrint (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);
2109
2110 FreePool(PostReplaceFormat);
2111
2112 if (Col != -1 && Row != -1) {
2113 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
2114 ASSERT_EFI_ERROR(Status);
2115 }
2116
2117 NormalAttribute = gST->ConOut->Mode->Attribute;
2118 FormatWalker = PostReplaceFormat2;
2119 while (*FormatWalker != L'\0') {
2120 //
2121 // Find the next attribute change request
2122 //
2123 ResumeLocation = StrStr(FormatWalker, L"%");
2124 if (ResumeLocation != NULL) {
2125 *ResumeLocation = L'\0';
2126 }
2127 //
2128 // print the current FormatWalker string
2129 //
2130 Status = gST->ConOut->OutputString(gST->ConOut, FormatWalker);
2131 ASSERT_EFI_ERROR(Status);
2132 //
2133 // update the attribute
2134 //
2135 if (ResumeLocation != NULL) {
2136 switch (*(ResumeLocation+1)) {
2137 case (L'N'):
2138 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2139 break;
2140 case (L'E'):
2141 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2142 break;
2143 case (L'H'):
2144 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2145 break;
2146 case (L'B'):
2147 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2148 break;
2149 case (L'V'):
2150 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2151 break;
2152 default:
2153 ASSERT(FALSE);
2154 break;
2155 }
2156 } else {
2157 //
2158 // reset to normal now...
2159 //
2160 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2161 break;
2162 }
2163
2164 //
2165 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2166 //
2167 FormatWalker = ResumeLocation + 2;
2168 }
2169
2170 FreePool(PostReplaceFormat2);
2171
2172 return (Return);
2173 }