2 EFI_FILE_PROTOCOL wrappers for other items (Like Environment Variables,
3 StdIn, StdOut, StdErr, etc...).
5 Copyright 2016 Dell Inc.
6 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
7 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "FileHandleInternal.h"
21 #define MEM_WRITE_REALLOC_OVERHEAD 1024
24 File style interface for console (Open).
26 @param[in] This Ignored.
27 @param[out] NewHandle Ignored.
28 @param[in] FileName Ignored.
29 @param[in] OpenMode Ignored.
30 @param[in] Attributes Ignored.
36 FileInterfaceOpenNotFound(
37 IN EFI_FILE_PROTOCOL
*This
,
38 OUT EFI_FILE_PROTOCOL
**NewHandle
,
44 return (EFI_NOT_FOUND
);
48 File style interface for console (Close, Delete, & Flush)
50 @param[in] This Ignored.
56 FileInterfaceNopGeneric(
57 IN EFI_FILE_PROTOCOL
*This
64 File style interface for console (GetPosition).
66 @param[in] This Ignored.
67 @param[out] Position Ignored.
69 @retval EFI_UNSUPPORTED
73 FileInterfaceNopGetPosition(
74 IN EFI_FILE_PROTOCOL
*This
,
78 return (EFI_UNSUPPORTED
);
82 File style interface for console (SetPosition).
84 @param[in] This Ignored.
85 @param[in] Position Ignored.
87 @retval EFI_UNSUPPORTED
91 FileInterfaceNopSetPosition(
92 IN EFI_FILE_PROTOCOL
*This
,
96 return (EFI_UNSUPPORTED
);
100 File style interface for console (GetInfo).
102 @param[in] This Ignored.
103 @param[in] InformationType Ignored.
104 @param[in, out] BufferSize Ignored.
105 @param[out] Buffer Ignored.
107 @retval EFI_UNSUPPORTED
111 FileInterfaceNopGetInfo(
112 IN EFI_FILE_PROTOCOL
*This
,
113 IN EFI_GUID
*InformationType
,
114 IN OUT UINTN
*BufferSize
,
118 return (EFI_UNSUPPORTED
);
122 File style interface for console (SetInfo).
124 @param[in] This Ignored.
125 @param[in] InformationType Ignored.
126 @param[in] BufferSize Ignored.
127 @param[in] Buffer Ignored.
129 @retval EFI_UNSUPPORTED
133 FileInterfaceNopSetInfo(
134 IN EFI_FILE_PROTOCOL
*This
,
135 IN EFI_GUID
*InformationType
,
140 return (EFI_UNSUPPORTED
);
144 File style interface for StdOut (Write).
146 Writes data to the screen.
148 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
149 @param[in, out] BufferSize Size in bytes of Buffer.
150 @param[in] Buffer The pointer to the buffer to write.
152 @retval EFI_UNSUPPORTED No output console is supported.
153 @return A return value from gST->ConOut->OutputString.
157 FileInterfaceStdOutWrite(
158 IN EFI_FILE_PROTOCOL
*This
,
159 IN OUT UINTN
*BufferSize
,
163 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
) {
164 return (EFI_UNSUPPORTED
);
166 if (*((CHAR16
*)Buffer
) == gUnicodeFileTag
) {
167 return (gST
->ConOut
->OutputString(gST
->ConOut
, (CHAR16
*)Buffer
+ 1));
169 return (gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
));
173 File style interface for StdIn (Write).
175 @param[in] This Ignored.
176 @param[in, out] BufferSize Ignored.
177 @param[in] Buffer Ignored.
179 @retval EFI_UNSUPPORTED
183 FileInterfaceStdInWrite(
184 IN EFI_FILE_PROTOCOL
*This
,
185 IN OUT UINTN
*BufferSize
,
189 return (EFI_UNSUPPORTED
);
193 File style interface for console StdErr (Write).
195 Writes error to the error output.
197 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
198 @param[in, out] BufferSize Size in bytes of Buffer.
199 @param[in] Buffer The pointer to the buffer to write.
201 @return A return value from gST->StdErr->OutputString.
205 FileInterfaceStdErrWrite(
206 IN EFI_FILE_PROTOCOL
*This
,
207 IN OUT UINTN
*BufferSize
,
211 return (gST
->StdErr
->OutputString(gST
->StdErr
, Buffer
));
215 File style interface for console StdOut (Read).
217 @param[in] This Ignored.
218 @param[in, out] BufferSize Ignored.
219 @param[out] Buffer Ignored.
221 @retval EFI_UNSUPPORTED
225 FileInterfaceStdOutRead(
226 IN EFI_FILE_PROTOCOL
*This
,
227 IN OUT UINTN
*BufferSize
,
231 return (EFI_UNSUPPORTED
);
235 File style interface for console StdErr (Read).
237 @param[in] This Ignored.
238 @param[in, out] BufferSize Ignored.
239 @param[out] Buffer Ignored.
241 @retval EFI_UNSUPPORTED Always.
245 FileInterfaceStdErrRead(
246 IN EFI_FILE_PROTOCOL
*This
,
247 IN OUT UINTN
*BufferSize
,
251 return (EFI_UNSUPPORTED
);
255 File style interface for NUL file (Read).
257 @param[in] This Ignored.
258 @param[in, out] BufferSize Poiner to 0 upon return.
259 @param[out] Buffer Ignored.
261 @retval EFI_SUCCESS Always.
265 FileInterfaceNulRead(
266 IN EFI_FILE_PROTOCOL
*This
,
267 IN OUT UINTN
*BufferSize
,
272 return (EFI_SUCCESS
);
276 File style interface for NUL file (Write).
278 @param[in] This Ignored.
279 @param[in, out] BufferSize Ignored.
280 @param[in] Buffer Ignored.
286 FileInterfaceNulWrite(
287 IN EFI_FILE_PROTOCOL
*This
,
288 IN OUT UINTN
*BufferSize
,
292 return (EFI_SUCCESS
);
296 File style interface for console (Read).
298 This will return a single line of input from the console.
300 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the
301 file handle to read data from. Not used.
302 @param BufferSize On input, the size of the Buffer. On output, the amount
303 of data returned in Buffer. In both cases, the size is
305 @param Buffer The buffer into which the data is read.
308 @retval EFI_SUCCESS The data was read.
309 @retval EFI_NO_MEDIA The device has no medium.
310 @retval EFI_DEVICE_ERROR The device reported an error.
311 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.
312 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.
313 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
314 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory
315 entry. BufferSize has been updated with the size
316 needed to complete the request.
317 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
321 FileInterfaceStdInRead(
322 IN EFI_FILE_PROTOCOL
*This
,
323 IN OUT UINTN
*BufferSize
,
327 CHAR16
*CurrentString
;
329 UINTN Column
; // Column of current cursor
330 UINTN Row
; // Row of current cursor
331 UINTN StartColumn
; // Column at the beginning of the line
332 UINTN Update
; // Line index for update
333 UINTN Delete
; // Num of chars to delete from console after update
334 UINTN StringLen
; // Total length of the line
335 UINTN StringCurPos
; // Line index corresponding to the cursor
336 UINTN MaxStr
; // Maximum possible line length
338 UINTN TotalColumn
; // Num of columns in the console
339 UINTN TotalRow
; // Num of rows in the console
341 UINTN OutputLength
; // Length of the update string
342 UINTN TailRow
; // Row of end of line
343 UINTN TailColumn
; // Column of end of line
346 BUFFER_LIST
*LinePos
;
350 BOOLEAN InTabScrolling
; // Whether in TAB-completion state
351 EFI_SHELL_FILE_INFO
*FoundFileList
;
352 EFI_SHELL_FILE_INFO
*TabLinePos
;
353 EFI_SHELL_FILE_INFO
*TempPos
;
355 CHAR16
*TabOutputStr
;
356 BOOLEAN InQuotationMode
;
358 UINTN TabPos
; // Start index of the string to search for TAB completion.
359 UINTN TabUpdatePos
; // Start index of the string updated by TAB stroke
365 // If buffer is not large enough to hold a CHAR16, return minimum buffer size
367 if (*BufferSize
< sizeof (CHAR16
) * 2) {
368 *BufferSize
= sizeof (CHAR16
) * 2;
369 return (EFI_BUFFER_TOO_SMALL
);
373 CurrentString
= Buffer
;
379 LinePos
= NewPos
= (BUFFER_LIST
*)(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
381 InTabScrolling
= FALSE
;
382 Status
= EFI_SUCCESS
;
384 FoundFileList
= NULL
;
392 TabStr
= AllocateZeroPool (*BufferSize
);
393 if (TabStr
== NULL
) {
394 return EFI_OUT_OF_RESOURCES
;
396 TabOutputStr
= AllocateZeroPool (*BufferSize
);
397 if (TabOutputStr
== NULL
) {
399 return EFI_OUT_OF_RESOURCES
;
403 // Get the screen setting and the current cursor location
405 Column
= StartColumn
= gST
->ConOut
->Mode
->CursorColumn
;
406 Row
= gST
->ConOut
->Mode
->CursorRow
;
407 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &TotalColumn
, &TotalRow
);
410 // Limit the line length to the buffer size or the minimun size of the
411 // screen. (The smaller takes effect)
413 MaxStr
= TotalColumn
* (TotalRow
- 1) - StartColumn
;
414 if (MaxStr
> *BufferSize
/ sizeof (CHAR16
)) {
415 MaxStr
= *BufferSize
/ sizeof (CHAR16
);
417 ZeroMem (CurrentString
, MaxStr
* sizeof (CHAR16
));
422 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
423 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
424 if (EFI_ERROR (Status
)) {
429 // Press PageUp or PageDown to scroll the history screen up or down.
430 // Press any other key to quit scrolling.
432 if (Key
.UnicodeChar
== 0 && (Key
.ScanCode
== SCAN_PAGE_UP
|| Key
.ScanCode
== SCAN_PAGE_DOWN
)) {
433 if (Key
.ScanCode
== SCAN_PAGE_UP
) {
434 ConsoleLoggerDisplayHistory(FALSE
, 0, ShellInfoObject
.ConsoleInfo
);
435 } else if (Key
.ScanCode
== SCAN_PAGE_DOWN
) {
436 ConsoleLoggerDisplayHistory(TRUE
, 0, ShellInfoObject
.ConsoleInfo
);
442 ConsoleLoggerStopHistory(ShellInfoObject
.ConsoleInfo
);
448 // If we are quitting TAB scrolling...
450 if (InTabScrolling
&& Key
.UnicodeChar
!= CHAR_TAB
) {
451 if (FoundFileList
!= NULL
) {
452 ShellInfoObject
.NewEfiShellProtocol
->FreeFileList (&FoundFileList
);
453 DEBUG_CODE(FoundFileList
= NULL
;);
455 InTabScrolling
= FALSE
;
458 switch (Key
.UnicodeChar
) {
459 case CHAR_CARRIAGE_RETURN
:
461 // All done, print a newline at the end of the string
463 TailRow
= Row
+ (StringLen
- StringCurPos
+ Column
) / TotalColumn
;
464 TailColumn
= (StringLen
- StringCurPos
+ Column
) % TotalColumn
;
465 ShellPrintEx ((INT32
)TailColumn
, (INT32
)TailRow
, L
"%N\n");
470 if (StringCurPos
!= 0) {
472 // If not move back beyond string beginning, move all characters behind
473 // the current position one character forward
476 Update
= StringCurPos
;
478 CopyMem (CurrentString
+ StringCurPos
, CurrentString
+ StringCurPos
+ 1, sizeof (CHAR16
) * (StringLen
- StringCurPos
));
481 // Adjust the current column and row
483 MoveCursorBackward (TotalColumn
, &Column
, &Row
);
489 // handle auto complete of file and directory names...
491 if (InTabScrolling
) {
492 ASSERT(FoundFileList
!= NULL
);
493 ASSERT(TabLinePos
!= NULL
);
494 TabLinePos
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(FoundFileList
->Link
), &TabLinePos
->Link
);
495 if (IsNull(&(FoundFileList
->Link
), &TabLinePos
->Link
)) {
496 TabLinePos
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(FoundFileList
->Link
), &TabLinePos
->Link
);
501 InQuotationMode
= FALSE
;
502 for (Index
= 0; Index
< StringLen
; Index
++) {
503 if (CurrentString
[Index
] == L
'\"') {
504 InQuotationMode
= (BOOLEAN
)(!InQuotationMode
);
506 if (CurrentString
[Index
] == L
' ' && !InQuotationMode
) {
508 TabUpdatePos
= Index
+ 1;
510 if (CurrentString
[Index
] == L
'\\') {
511 TabUpdatePos
= Index
+ 1;
514 if (StrStr(CurrentString
+ TabPos
, L
":") == NULL
) {
515 Cwd
= ShellInfoObject
.NewEfiShellProtocol
->GetCurDir(NULL
);
517 StrnCpyS(TabStr
, (*BufferSize
)/sizeof(CHAR16
), Cwd
, (*BufferSize
)/sizeof(CHAR16
) - 1);
518 StrCatS(TabStr
, (*BufferSize
)/sizeof(CHAR16
), L
"\\");
519 if (TabStr
[StrLen(TabStr
)-1] == L
'\\' && *(CurrentString
+ TabPos
) == L
'\\' ) {
520 TabStr
[StrLen(TabStr
)-1] = CHAR_NULL
;
523 (*BufferSize
)/sizeof(CHAR16
),
524 CurrentString
+ TabPos
,
529 StrnCatS(TabStr
, (*BufferSize
)/sizeof(CHAR16
), CurrentString
+ TabPos
, StringLen
- TabPos
);
532 StrnCpyS(TabStr
, (*BufferSize
)/sizeof(CHAR16
), CurrentString
+ TabPos
, (*BufferSize
)/sizeof(CHAR16
) - 1);
534 StrnCatS(TabStr
, (*BufferSize
)/sizeof(CHAR16
), L
"*", (*BufferSize
)/sizeof(CHAR16
) - 1 - StrLen(TabStr
));
535 FoundFileList
= NULL
;
536 Status
= ShellInfoObject
.NewEfiShellProtocol
->FindFiles(TabStr
, &FoundFileList
);
537 for ( TempStr
= CurrentString
539 ; TempStr
++); // note the ';'... empty for loop
541 // make sure we have a list before we do anything more...
543 if (EFI_ERROR (Status
) || FoundFileList
== NULL
) {
544 InTabScrolling
= FALSE
;
549 // enumerate through the list of files
551 for ( TempPos
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(FoundFileList
->Link
))
552 ; !IsNull(&FoundFileList
->Link
, &TempPos
->Link
)
553 ; TempPos
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(FoundFileList
->Link
), &(TempPos
->Link
))
556 // If "cd" is typed, only directory name will be auto-complete filled
557 // in either case . and .. will be removed.
559 if ((((TempStr
[0] == L
'c' || TempStr
[0] == L
'C') &&
560 (TempStr
[1] == L
'd' || TempStr
[1] == L
'D')
561 ) && ((ShellIsDirectory(TempPos
->FullName
) != EFI_SUCCESS
)
562 ||(StrCmp(TempPos
->FileName
, L
".") == 0)
563 ||(StrCmp(TempPos
->FileName
, L
"..") == 0)
564 )) || ((StrCmp(TempPos
->FileName
, L
".") == 0)
565 ||(StrCmp(TempPos
->FileName
, L
"..") == 0))){
566 TabLinePos
= TempPos
;
567 TempPos
= (EFI_SHELL_FILE_INFO
*)(RemoveEntryList(&(TempPos
->Link
))->BackLink
);
568 InternalFreeShellFileInfoNode(TabLinePos
);
571 if (FoundFileList
!= NULL
&& !IsListEmpty(&FoundFileList
->Link
)) {
572 TabLinePos
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FoundFileList
->Link
);
573 InTabScrolling
= TRUE
;
575 ShellInfoObject
.NewEfiShellProtocol
->FreeFileList (&FoundFileList
);
582 if (Key
.UnicodeChar
>= ' ') {
584 // If we are at the buffer's end, drop the key
586 if (StringLen
== MaxStr
- 1 && (ShellInfoObject
.ViewingSettings
.InsertMode
|| StringCurPos
== StringLen
)) {
590 // If in insert mode, make space by moving each other character 1
591 // space higher in the array
593 if (ShellInfoObject
.ViewingSettings
.InsertMode
) {
594 CopyMem(CurrentString
+ StringCurPos
+ 1, CurrentString
+ StringCurPos
, (StringLen
- StringCurPos
)*sizeof(CurrentString
[0]));
597 CurrentString
[StringCurPos
] = Key
.UnicodeChar
;
598 Update
= StringCurPos
;
606 switch (Key
.ScanCode
) {
609 // Move characters behind current position one character forward
611 if (StringLen
!= 0) {
612 Update
= StringCurPos
;
614 CopyMem (CurrentString
+ StringCurPos
, CurrentString
+ StringCurPos
+ 1, sizeof (CHAR16
) * (StringLen
- StringCurPos
));
620 // Prepare to print the previous command
622 NewPos
= (BUFFER_LIST
*)GetPreviousNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &LinePos
->Link
);
623 if (IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &LinePos
->Link
)) {
624 NewPos
= (BUFFER_LIST
*)GetPreviousNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &LinePos
->Link
);
630 // Prepare to print the next command
632 NewPos
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &LinePos
->Link
);
633 if (NewPos
== (BUFFER_LIST
*)(&ShellInfoObject
.ViewingSettings
.CommandHistory
)) {
634 NewPos
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &LinePos
->Link
);
640 // Adjust current cursor position
642 if (StringCurPos
!= 0) {
644 MoveCursorBackward (TotalColumn
, &Column
, &Row
);
650 // Adjust current cursor position
652 if (StringCurPos
< StringLen
) {
654 MoveCursorForward (TotalColumn
, TotalRow
, &Column
, &Row
);
660 // Move current cursor position to the beginning of the command line
662 Row
-= (StringCurPos
+ StartColumn
) / TotalColumn
;
663 Column
= StartColumn
;
669 // Move current cursor position to the end of the command line
671 TailRow
= Row
+ (StringLen
- StringCurPos
+ Column
) / TotalColumn
;
672 TailColumn
= (StringLen
- StringCurPos
+ Column
) % TotalColumn
;
675 StringCurPos
= StringLen
;
680 // Prepare to clear the current command line
682 CurrentString
[0] = 0;
685 Row
-= (StringCurPos
+ StartColumn
) / TotalColumn
;
686 Column
= StartColumn
;
692 // Toggle the SEnvInsertMode flag
694 ShellInfoObject
.ViewingSettings
.InsertMode
= (BOOLEAN
)!ShellInfoObject
.ViewingSettings
.InsertMode
;
699 // Print command history
701 PrintCommandHistory (TotalColumn
, TotalRow
, 4);
702 *CurrentString
= CHAR_NULL
;
713 // If we are in auto-complete mode, we are preparing to print
714 // the next file or directory name
716 if (InTabScrolling
) {
718 // Adjust the column and row to the start of TAB-completion string.
720 Column
= (StartColumn
+ TabUpdatePos
) % TotalColumn
;
721 Row
-= (StartColumn
+ StringCurPos
) / TotalColumn
- (StartColumn
+ TabUpdatePos
) / TotalColumn
;
722 OutputLength
= StrLen (TabLinePos
->FileName
);
724 // if the output string contains blank space, quotation marks L'\"'
725 // should be added to the output.
727 if (StrStr(TabLinePos
->FileName
, L
" ") != NULL
){
728 TabOutputStr
[0] = L
'\"';
729 CopyMem (TabOutputStr
+ 1, TabLinePos
->FileName
, OutputLength
* sizeof (CHAR16
));
730 TabOutputStr
[OutputLength
+ 1] = L
'\"';
731 TabOutputStr
[OutputLength
+ 2] = CHAR_NULL
;
733 CopyMem (TabOutputStr
, TabLinePos
->FileName
, OutputLength
* sizeof (CHAR16
));
734 TabOutputStr
[OutputLength
] = CHAR_NULL
;
736 OutputLength
= StrLen (TabOutputStr
) < MaxStr
- 1 ? StrLen (TabOutputStr
) : MaxStr
- 1;
737 CopyMem (CurrentString
+ TabUpdatePos
, TabOutputStr
, OutputLength
* sizeof (CHAR16
));
738 CurrentString
[TabUpdatePos
+ OutputLength
] = CHAR_NULL
;
739 StringCurPos
= TabUpdatePos
+ OutputLength
;
740 Update
= TabUpdatePos
;
741 if (StringLen
> TabUpdatePos
+ OutputLength
) {
742 Delete
= StringLen
- TabUpdatePos
- OutputLength
;
747 // If we have a new position, we are preparing to print a previous or
750 if (NewPos
!= (BUFFER_LIST
*)(&ShellInfoObject
.ViewingSettings
.CommandHistory
)) {
751 Column
= StartColumn
;
752 Row
-= (StringCurPos
+ StartColumn
) / TotalColumn
;
755 NewPos
= (BUFFER_LIST
*)(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
757 OutputLength
= StrLen (LinePos
->Buffer
) < MaxStr
- 1 ? StrLen (LinePos
->Buffer
) : MaxStr
- 1;
758 CopyMem (CurrentString
, LinePos
->Buffer
, OutputLength
* sizeof (CHAR16
));
759 CurrentString
[OutputLength
] = CHAR_NULL
;
761 StringCurPos
= OutputLength
;
764 // Draw new input string
767 if (StringLen
> OutputLength
) {
769 // If old string was longer, blank its tail
771 Delete
= StringLen
- OutputLength
;
775 // If we need to update the output do so now
777 if (Update
!= (UINTN
) -1) {
778 ShellPrintEx ((INT32
)Column
, (INT32
)Row
, L
"%s%.*s", CurrentString
+ Update
, Delete
, L
"");
779 StringLen
= StrLen (CurrentString
);
782 SetMem (CurrentString
+ StringLen
, Delete
* sizeof (CHAR16
), CHAR_NULL
);
785 if (StringCurPos
> StringLen
) {
786 StringCurPos
= StringLen
;
792 // After using print to reflect newly updates, if we're not using
793 // BACKSPACE and DELETE, we need to move the cursor position forward,
794 // so adjust row and column here.
796 if (Key
.UnicodeChar
!= CHAR_BACKSPACE
&& !(Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_DELETE
)) {
798 // Calulate row and column of the tail of current string
800 TailRow
= Row
+ (StringLen
- StringCurPos
+ Column
+ OutputLength
) / TotalColumn
;
801 TailColumn
= (StringLen
- StringCurPos
+ Column
+ OutputLength
) % TotalColumn
;
804 // If the tail of string reaches screen end, screen rolls up, so if
805 // Row does not equal TailRow, Row should be decremented
807 // (if we are recalling commands using UPPER and DOWN key, and if the
808 // old command is too long to fit the screen, TailColumn must be 79.
810 if (TailColumn
== 0 && TailRow
>= TotalRow
&& Row
!= TailRow
) {
814 // Calculate the cursor position after current operation. If cursor
815 // reaches line end, update both row and column, otherwise, only
816 // column will be changed.
818 if (Column
+ OutputLength
>= TotalColumn
) {
819 SkipLength
= OutputLength
- (TotalColumn
- Column
);
821 Row
+= SkipLength
/ TotalColumn
+ 1;
822 if (Row
> TotalRow
- 1) {
826 Column
= SkipLength
% TotalColumn
;
828 Column
+= OutputLength
;
835 // Set the cursor position for this key
837 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, Column
, Row
);
840 if (CurrentString
!= NULL
&& StrLen(CurrentString
) > 0) {
842 // add the line to the history buffer
844 AddLineToCommandHistory(CurrentString
);
848 FreePool (TabOutputStr
);
850 // Return the data to the caller
852 *BufferSize
= StringLen
* sizeof (CHAR16
);
855 // if this was used it should be deallocated by now...
856 // prevent memory leaks...
858 if (FoundFileList
!= NULL
) {
859 ShellInfoObject
.NewEfiShellProtocol
->FreeFileList (&FoundFileList
);
861 ASSERT(FoundFileList
== NULL
);
867 // FILE sytle interfaces for StdIn/StdOut/StdErr
869 EFI_FILE_PROTOCOL FileInterfaceStdIn
= {
871 FileInterfaceOpenNotFound
,
872 FileInterfaceNopGeneric
,
873 FileInterfaceNopGeneric
,
874 FileInterfaceStdInRead
,
875 FileInterfaceStdInWrite
,
876 FileInterfaceNopGetPosition
,
877 FileInterfaceNopSetPosition
,
878 FileInterfaceNopGetInfo
,
879 FileInterfaceNopSetInfo
,
880 FileInterfaceNopGeneric
883 EFI_FILE_PROTOCOL FileInterfaceStdOut
= {
885 FileInterfaceOpenNotFound
,
886 FileInterfaceNopGeneric
,
887 FileInterfaceNopGeneric
,
888 FileInterfaceStdOutRead
,
889 FileInterfaceStdOutWrite
,
890 FileInterfaceNopGetPosition
,
891 FileInterfaceNopSetPosition
,
892 FileInterfaceNopGetInfo
,
893 FileInterfaceNopSetInfo
,
894 FileInterfaceNopGeneric
897 EFI_FILE_PROTOCOL FileInterfaceStdErr
= {
899 FileInterfaceOpenNotFound
,
900 FileInterfaceNopGeneric
,
901 FileInterfaceNopGeneric
,
902 FileInterfaceStdErrRead
,
903 FileInterfaceStdErrWrite
,
904 FileInterfaceNopGetPosition
,
905 FileInterfaceNopSetPosition
,
906 FileInterfaceNopGetInfo
,
907 FileInterfaceNopSetInfo
,
908 FileInterfaceNopGeneric
911 EFI_FILE_PROTOCOL FileInterfaceNulFile
= {
913 FileInterfaceOpenNotFound
,
914 FileInterfaceNopGeneric
,
915 FileInterfaceNopGeneric
,
916 FileInterfaceNulRead
,
917 FileInterfaceNulWrite
,
918 FileInterfaceNopGetPosition
,
919 FileInterfaceNopSetPosition
,
920 FileInterfaceNopGetInfo
,
921 FileInterfaceNopSetInfo
,
922 FileInterfaceNopGeneric
929 // This is identical to EFI_FILE_PROTOCOL except for the additional member
936 EFI_FILE_CLOSE Close
;
937 EFI_FILE_DELETE Delete
;
939 EFI_FILE_WRITE Write
;
940 EFI_FILE_GET_POSITION GetPosition
;
941 EFI_FILE_SET_POSITION SetPosition
;
942 EFI_FILE_GET_INFO GetInfo
;
943 EFI_FILE_SET_INFO SetInfo
;
944 EFI_FILE_FLUSH Flush
;
946 } EFI_FILE_PROTOCOL_ENVIRONMENT
;
947 //ANSI compliance helper to get size of the struct.
948 #define SIZE_OF_EFI_FILE_PROTOCOL_ENVIRONMENT EFI_FIELD_OFFSET (EFI_FILE_PROTOCOL_ENVIRONMENT, Name)
951 File style interface for Environment Variable (Close).
953 Frees the memory for this object.
955 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
961 FileInterfaceEnvClose(
962 IN EFI_FILE_PROTOCOL
*This
970 // Most if not all UEFI commands will have an '\r\n' at the end of any output.
971 // Since the output was redirected to a variable, it does not make sense to
972 // keep this. So, before closing, strip the trailing '\r\n' from the variable
978 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
979 if (Status
== EFI_BUFFER_TOO_SMALL
) {
980 NewBuffer
= AllocateZeroPool(NewSize
+ sizeof(CHAR16
));
981 if (NewBuffer
== NULL
) {
982 return EFI_OUT_OF_RESOURCES
;
984 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
987 if (!EFI_ERROR(Status
) && NewBuffer
!= NULL
) {
989 if (StrSize(NewBuffer
) > 6)
991 if ((((CHAR16
*)NewBuffer
)[(StrSize(NewBuffer
)/2) - 2] == CHAR_LINEFEED
)
992 && (((CHAR16
*)NewBuffer
)[(StrSize(NewBuffer
)/2) - 3] == CHAR_CARRIAGE_RETURN
)) {
993 ((CHAR16
*)NewBuffer
)[(StrSize(NewBuffer
)/2) - 3] = CHAR_NULL
;
996 if (IsVolatileEnv(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
)) {
997 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, StrSize(NewBuffer
), NewBuffer
);
999 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, StrSize(NewBuffer
), NewBuffer
);
1004 SHELL_FREE_NON_NULL(NewBuffer
);
1005 FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
);
1010 File style interface for Environment Variable (Delete).
1012 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1014 @retval The return value from FileInterfaceEnvClose().
1018 FileInterfaceEnvDelete(
1019 IN EFI_FILE_PROTOCOL
*This
1022 SHELL_DELETE_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
);
1023 return (FileInterfaceEnvClose(This
));
1027 File style interface for Environment Variable (Read).
1029 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1030 @param[in, out] BufferSize Size in bytes of Buffer.
1031 @param[out] Buffer The pointer to the buffer to fill.
1033 @retval EFI_SUCCESS The data was read.
1037 FileInterfaceEnvRead(
1038 IN EFI_FILE_PROTOCOL
*This
,
1039 IN OUT UINTN
*BufferSize
,
1043 return (SHELL_GET_ENVIRONMENT_VARIABLE(
1044 ((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
,
1050 File style interface for Volatile Environment Variable (Write).
1052 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1053 @param[in, out] BufferSize Size in bytes of Buffer.
1054 @param[in] Buffer The pointer to the buffer to write.
1056 @retval EFI_SUCCESS The data was read.
1060 FileInterfaceEnvVolWrite(
1061 IN EFI_FILE_PROTOCOL
*This
,
1062 IN OUT UINTN
*BufferSize
,
1073 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
1074 if (Status
== EFI_BUFFER_TOO_SMALL
){
1075 NewBuffer
= AllocateZeroPool(NewSize
+ *BufferSize
+ sizeof(CHAR16
));
1076 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
1078 if (!EFI_ERROR(Status
) && NewBuffer
!= NULL
) {
1079 while (((CHAR16
*)NewBuffer
)[NewSize
/2] == CHAR_NULL
) {
1081 // We want to overwrite the CHAR_NULL
1085 CopyMem((UINT8
*)NewBuffer
+ NewSize
+ 2, Buffer
, *BufferSize
);
1086 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, StrSize(NewBuffer
), NewBuffer
);
1087 FreePool(NewBuffer
);
1090 SHELL_FREE_NON_NULL(NewBuffer
);
1091 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, *BufferSize
, Buffer
));
1097 File style interface for Non Volatile Environment Variable (Write).
1099 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1100 @param[in, out] BufferSize Size in bytes of Buffer.
1101 @param[in] Buffer The pointer to the buffer to write.
1103 @retval EFI_SUCCESS The data was read.
1107 FileInterfaceEnvNonVolWrite(
1108 IN EFI_FILE_PROTOCOL
*This
,
1109 IN OUT UINTN
*BufferSize
,
1120 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
1121 if (Status
== EFI_BUFFER_TOO_SMALL
){
1122 NewBuffer
= AllocateZeroPool(NewSize
+ *BufferSize
);
1123 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
, &NewSize
, NewBuffer
);
1125 if (!EFI_ERROR(Status
)) {
1126 CopyMem((UINT8
*)NewBuffer
+ NewSize
, Buffer
, *BufferSize
);
1127 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(
1128 ((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
,
1129 NewSize
+ *BufferSize
,
1132 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(
1133 ((EFI_FILE_PROTOCOL_ENVIRONMENT
*)This
)->Name
,
1140 Creates a EFI_FILE_PROTOCOL (almost) object for using to access
1141 environment variables through file operations.
1143 @param EnvName The name of the Environment Variable to be operated on.
1145 @retval NULL Memory could not be allocated.
1146 @return other a pointer to an EFI_FILE_PROTOCOL structure
1150 CreateFileInterfaceEnv(
1151 IN CONST CHAR16
*EnvName
1154 EFI_FILE_PROTOCOL_ENVIRONMENT
*EnvFileInterface
;
1157 if (EnvName
== NULL
) {
1164 EnvNameSize
= StrSize(EnvName
);
1165 EnvFileInterface
= AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT
)+EnvNameSize
);
1166 if (EnvFileInterface
== NULL
){
1171 // Assign the generic members
1173 EnvFileInterface
->Revision
= EFI_FILE_REVISION
;
1174 EnvFileInterface
->Open
= FileInterfaceOpenNotFound
;
1175 EnvFileInterface
->Close
= FileInterfaceEnvClose
;
1176 EnvFileInterface
->GetPosition
= FileInterfaceNopGetPosition
;
1177 EnvFileInterface
->SetPosition
= FileInterfaceNopSetPosition
;
1178 EnvFileInterface
->GetInfo
= FileInterfaceNopGetInfo
;
1179 EnvFileInterface
->SetInfo
= FileInterfaceNopSetInfo
;
1180 EnvFileInterface
->Flush
= FileInterfaceNopGeneric
;
1181 EnvFileInterface
->Delete
= FileInterfaceEnvDelete
;
1182 EnvFileInterface
->Read
= FileInterfaceEnvRead
;
1184 CopyMem(EnvFileInterface
->Name
, EnvName
, EnvNameSize
);
1187 // Assign the different members for Volatile and Non-Volatile variables
1189 if (IsVolatileEnv(EnvName
)) {
1190 EnvFileInterface
->Write
= FileInterfaceEnvVolWrite
;
1192 EnvFileInterface
->Write
= FileInterfaceEnvNonVolWrite
;
1194 return ((EFI_FILE_PROTOCOL
*)EnvFileInterface
);
1198 Move the cursor position one character backward.
1200 @param[in] LineLength Length of a line. Get it by calling QueryMode
1201 @param[in, out] Column Current column of the cursor position
1202 @param[in, out] Row Current row of the cursor position
1206 MoveCursorBackward (
1207 IN UINTN LineLength
,
1208 IN OUT UINTN
*Column
,
1213 // If current column is 0, move to the last column of the previous line,
1214 // otherwise, just decrement column.
1217 *Column
= LineLength
- 1;
1227 Move the cursor position one character forward.
1229 @param[in] LineLength Length of a line.
1230 @param[in] TotalRow Total row of a screen
1231 @param[in, out] Column Current column of the cursor position
1232 @param[in, out] Row Current row of the cursor position
1237 IN UINTN LineLength
,
1239 IN OUT UINTN
*Column
,
1244 // Increment Column.
1245 // If this puts column past the end of the line, move to first column
1249 if (*Column
>= LineLength
) {
1251 if ((*Row
) < TotalRow
- 1) {
1258 Prints out each previously typed command in the command list history log.
1260 When each screen is full it will pause for a key before continuing.
1262 @param[in] TotalCols How many columns are on the screen
1263 @param[in] TotalRows How many rows are on the screen
1264 @param[in] StartColumn which column to start at
1268 PrintCommandHistory (
1269 IN CONST UINTN TotalCols
,
1270 IN CONST UINTN TotalRows
,
1271 IN CONST UINTN StartColumn
1279 ShellPrintEx (-1, -1, L
"\n");
1283 // go through history list...
1285 for ( Node
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1286 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
)
1287 ; Node
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
)
1290 LineCount
= ((StrLen (Node
->Buffer
) + StartColumn
+ 1) / TotalCols
) + 1;
1292 if (LineNumber
+ LineCount
>= TotalRows
) {
1293 ShellPromptForResponseHii(
1294 ShellPromptResponseTypeEnterContinue
,
1295 STRING_TOKEN (STR_SHELL_ENTER_TO_CONT
),
1296 ShellInfoObject
.HiiHandle
,
1301 ShellPrintEx (-1, -1, L
"%2d. %s\n", Index
, Node
->Buffer
);
1302 LineNumber
+= LineCount
;
1312 // This is identical to EFI_FILE_PROTOCOL except for the additional members
1313 // for the buffer, size, and position.
1319 EFI_FILE_CLOSE Close
;
1320 EFI_FILE_DELETE Delete
;
1322 EFI_FILE_WRITE Write
;
1323 EFI_FILE_GET_POSITION GetPosition
;
1324 EFI_FILE_SET_POSITION SetPosition
;
1325 EFI_FILE_GET_INFO GetInfo
;
1326 EFI_FILE_SET_INFO SetInfo
;
1327 EFI_FILE_FLUSH Flush
;
1333 } EFI_FILE_PROTOCOL_MEM
;
1336 File style interface for Mem (SetPosition).
1338 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1339 @param[out] Position The position to set.
1341 @retval EFI_SUCCESS The position was successfully changed.
1342 @retval EFI_INVALID_PARAMETER The Position was invalid.
1346 FileInterfaceMemSetPosition(
1347 IN EFI_FILE_PROTOCOL
*This
,
1351 if (Position
<= ((EFI_FILE_PROTOCOL_MEM
*)This
)->FileSize
) {
1352 ((EFI_FILE_PROTOCOL_MEM
*)This
)->Position
= Position
;
1353 return (EFI_SUCCESS
);
1355 return (EFI_INVALID_PARAMETER
);
1360 File style interface for Mem (GetPosition).
1362 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1363 @param[out] Position The pointer to the position.
1365 @retval EFI_SUCCESS The position was retrieved.
1369 FileInterfaceMemGetPosition(
1370 IN EFI_FILE_PROTOCOL
*This
,
1371 OUT UINT64
*Position
1374 *Position
= ((EFI_FILE_PROTOCOL_MEM
*)This
)->Position
;
1375 return (EFI_SUCCESS
);
1379 File style interface for Mem (Write).
1381 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1382 @param[in, out] BufferSize Size in bytes of Buffer.
1383 @param[in] Buffer The pointer to the buffer to write.
1385 @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.
1386 @retval EFI_SUCCESS The data was written.
1390 FileInterfaceMemWrite(
1391 IN EFI_FILE_PROTOCOL
*This
,
1392 IN OUT UINTN
*BufferSize
,
1397 EFI_FILE_PROTOCOL_MEM
*MemFile
;
1399 MemFile
= (EFI_FILE_PROTOCOL_MEM
*) This
;
1400 if (MemFile
->Unicode
) {
1404 if ((UINTN
)(MemFile
->Position
+ (*BufferSize
)) > (UINTN
)(MemFile
->BufferSize
)) {
1405 MemFile
->Buffer
= ReallocatePool((UINTN
)(MemFile
->BufferSize
), (UINTN
)(MemFile
->BufferSize
) + (*BufferSize
) + MEM_WRITE_REALLOC_OVERHEAD
, MemFile
->Buffer
);
1406 MemFile
->BufferSize
+= (*BufferSize
) + MEM_WRITE_REALLOC_OVERHEAD
;
1408 CopyMem(((UINT8
*)MemFile
->Buffer
) + MemFile
->Position
, Buffer
, *BufferSize
);
1409 MemFile
->Position
+= (*BufferSize
);
1410 MemFile
->FileSize
= MemFile
->Position
;
1411 return (EFI_SUCCESS
);
1416 AsciiBuffer
= AllocateZeroPool(*BufferSize
);
1417 if (AsciiBuffer
== NULL
) {
1418 return (EFI_OUT_OF_RESOURCES
);
1420 AsciiSPrint(AsciiBuffer
, *BufferSize
, "%S", Buffer
);
1421 if ((UINTN
)(MemFile
->Position
+ AsciiStrSize(AsciiBuffer
)) > (UINTN
)(MemFile
->BufferSize
)) {
1422 MemFile
->Buffer
= ReallocatePool((UINTN
)(MemFile
->BufferSize
), (UINTN
)(MemFile
->BufferSize
) + AsciiStrSize(AsciiBuffer
) + MEM_WRITE_REALLOC_OVERHEAD
, MemFile
->Buffer
);
1423 MemFile
->BufferSize
+= AsciiStrSize(AsciiBuffer
) + MEM_WRITE_REALLOC_OVERHEAD
;
1425 CopyMem(((UINT8
*)MemFile
->Buffer
) + MemFile
->Position
, AsciiBuffer
, AsciiStrSize(AsciiBuffer
));
1426 MemFile
->Position
+= (*BufferSize
/ sizeof(CHAR16
));
1427 MemFile
->FileSize
= MemFile
->Position
;
1428 FreePool(AsciiBuffer
);
1429 return (EFI_SUCCESS
);
1434 File style interface for Mem (Read).
1436 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1437 @param[in, out] BufferSize Size in bytes of Buffer.
1438 @param[in] Buffer The pointer to the buffer to fill.
1440 @retval EFI_SUCCESS The data was read.
1444 FileInterfaceMemRead(
1445 IN EFI_FILE_PROTOCOL
*This
,
1446 IN OUT UINTN
*BufferSize
,
1450 EFI_FILE_PROTOCOL_MEM
*MemFile
;
1452 MemFile
= (EFI_FILE_PROTOCOL_MEM
*) This
;
1453 if (*BufferSize
> (UINTN
)((MemFile
->FileSize
) - (UINTN
)(MemFile
->Position
))) {
1454 (*BufferSize
) = (UINTN
)((MemFile
->FileSize
) - (UINTN
)(MemFile
->Position
));
1456 CopyMem(Buffer
, ((UINT8
*)MemFile
->Buffer
) + MemFile
->Position
, (*BufferSize
));
1457 MemFile
->Position
= MemFile
->Position
+ (*BufferSize
);
1458 return (EFI_SUCCESS
);
1462 File style interface for Mem (Close).
1464 Frees all memory associated with this object.
1466 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1468 @retval EFI_SUCCESS The 'file' was closed.
1472 FileInterfaceMemClose(
1473 IN EFI_FILE_PROTOCOL
*This
1476 SHELL_FREE_NON_NULL(((EFI_FILE_PROTOCOL_MEM
*)This
)->Buffer
);
1477 SHELL_FREE_NON_NULL(This
);
1478 return (EFI_SUCCESS
);
1482 Creates a EFI_FILE_PROTOCOL (almost) object for using to access
1483 a file entirely in memory through file operations.
1485 @param[in] Unicode Boolean value with TRUE for Unicode and FALSE for Ascii.
1487 @retval NULL Memory could not be allocated.
1488 @return other A pointer to an EFI_FILE_PROTOCOL structure.
1492 CreateFileInterfaceMem(
1493 IN CONST BOOLEAN Unicode
1496 EFI_FILE_PROTOCOL_MEM
*FileInterface
;
1501 FileInterface
= AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_MEM
));
1502 if (FileInterface
== NULL
){
1507 // Assign the generic members
1509 FileInterface
->Revision
= EFI_FILE_REVISION
;
1510 FileInterface
->Open
= FileInterfaceOpenNotFound
;
1511 FileInterface
->Close
= FileInterfaceMemClose
;
1512 FileInterface
->GetPosition
= FileInterfaceMemGetPosition
;
1513 FileInterface
->SetPosition
= FileInterfaceMemSetPosition
;
1514 FileInterface
->GetInfo
= FileInterfaceNopGetInfo
;
1515 FileInterface
->SetInfo
= FileInterfaceNopSetInfo
;
1516 FileInterface
->Flush
= FileInterfaceNopGeneric
;
1517 FileInterface
->Delete
= FileInterfaceNopGeneric
;
1518 FileInterface
->Read
= FileInterfaceMemRead
;
1519 FileInterface
->Write
= FileInterfaceMemWrite
;
1520 FileInterface
->Unicode
= Unicode
;
1522 ASSERT(FileInterface
->Buffer
== NULL
);
1523 ASSERT(FileInterface
->BufferSize
== 0);
1524 ASSERT(FileInterface
->Position
== 0);
1527 FileInterface
->Buffer
= AllocateZeroPool(sizeof(gUnicodeFileTag
));
1528 if (FileInterface
->Buffer
== NULL
) {
1529 FreePool (FileInterface
);
1532 *((CHAR16
*) (FileInterface
->Buffer
)) = EFI_UNICODE_BYTE_ORDER_MARK
;
1533 FileInterface
->BufferSize
= 2;
1534 FileInterface
->Position
= 2;
1537 return ((EFI_FILE_PROTOCOL
*)FileInterface
);
1543 EFI_FILE_CLOSE Close
;
1544 EFI_FILE_DELETE Delete
;
1546 EFI_FILE_WRITE Write
;
1547 EFI_FILE_GET_POSITION GetPosition
;
1548 EFI_FILE_SET_POSITION SetPosition
;
1549 EFI_FILE_GET_INFO GetInfo
;
1550 EFI_FILE_SET_INFO SetInfo
;
1551 EFI_FILE_FLUSH Flush
;
1553 EFI_FILE_PROTOCOL
*Orig
;
1554 } EFI_FILE_PROTOCOL_FILE
;
1557 Set a files current position
1559 @param This Protocol instance pointer.
1560 @param Position Byte position from the start of the file.
1562 @retval EFI_SUCCESS Data was written.
1563 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.
1568 FileInterfaceFileSetPosition(
1569 IN EFI_FILE_PROTOCOL
*This
,
1573 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->SetPosition(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, Position
);
1577 Get a file's current position
1579 @param This Protocol instance pointer.
1580 @param Position Byte position from the start of the file.
1582 @retval EFI_SUCCESS Data was written.
1583 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..
1588 FileInterfaceFileGetPosition(
1589 IN EFI_FILE_PROTOCOL
*This
,
1590 OUT UINT64
*Position
1593 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->GetPosition(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, Position
);
1597 Get information about a file.
1599 @param This Protocol instance pointer.
1600 @param InformationType Type of information to return in Buffer.
1601 @param BufferSize On input size of buffer, on output amount of data in buffer.
1602 @param Buffer The buffer to return data.
1604 @retval EFI_SUCCESS Data was returned.
1605 @retval EFI_UNSUPPORT InformationType is not supported.
1606 @retval EFI_NO_MEDIA The device has no media.
1607 @retval EFI_DEVICE_ERROR The device reported an error.
1608 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1609 @retval EFI_WRITE_PROTECTED The device is write protected.
1610 @retval EFI_ACCESS_DENIED The file was open for read only.
1611 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.
1616 FileInterfaceFileGetInfo(
1617 IN EFI_FILE_PROTOCOL
*This
,
1618 IN EFI_GUID
*InformationType
,
1619 IN OUT UINTN
*BufferSize
,
1623 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->GetInfo(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, InformationType
, BufferSize
, Buffer
);
1627 Set information about a file
1629 @param This Protocol instance pointer.
1630 @param InformationType Type of information in Buffer.
1631 @param BufferSize Size of buffer.
1632 @param Buffer The data to write.
1634 @retval EFI_SUCCESS Data was returned.
1635 @retval EFI_UNSUPPORT InformationType is not supported.
1636 @retval EFI_NO_MEDIA The device has no media.
1637 @retval EFI_DEVICE_ERROR The device reported an error.
1638 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1639 @retval EFI_WRITE_PROTECTED The device is write protected.
1640 @retval EFI_ACCESS_DENIED The file was open for read only.
1645 FileInterfaceFileSetInfo(
1646 IN EFI_FILE_PROTOCOL
*This
,
1647 IN EFI_GUID
*InformationType
,
1648 IN UINTN BufferSize
,
1652 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->SetInfo(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, InformationType
, BufferSize
, Buffer
);
1656 Flush data back for the file handle.
1658 @param This Protocol instance pointer.
1660 @retval EFI_SUCCESS Data was written.
1661 @retval EFI_UNSUPPORT Writes to Open directory are not supported.
1662 @retval EFI_NO_MEDIA The device has no media.
1663 @retval EFI_DEVICE_ERROR The device reported an error.
1664 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1665 @retval EFI_WRITE_PROTECTED The device is write protected.
1666 @retval EFI_ACCESS_DENIED The file was open for read only.
1667 @retval EFI_VOLUME_FULL The volume is full.
1672 FileInterfaceFileFlush(
1673 IN EFI_FILE_PROTOCOL
*This
1676 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Flush(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
);
1680 Read data from the file.
1682 @param This Protocol instance pointer.
1683 @param BufferSize On input size of buffer, on output amount of data in buffer.
1684 @param Buffer The buffer in which data is read.
1686 @retval EFI_SUCCESS Data was read.
1687 @retval EFI_NO_MEDIA The device has no media.
1688 @retval EFI_DEVICE_ERROR The device reported an error.
1689 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1690 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.
1695 FileInterfaceFileRead(
1696 IN EFI_FILE_PROTOCOL
*This
,
1697 IN OUT UINTN
*BufferSize
,
1701 CHAR8
*AsciiStrBuffer
;
1702 CHAR16
*UscStrBuffer
;
1706 if (((EFI_FILE_PROTOCOL_FILE
*)This
)->Unicode
) {
1710 return (((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Read(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, BufferSize
, Buffer
));
1715 Size
= (*BufferSize
) / sizeof(CHAR16
);
1716 AsciiStrBuffer
= AllocateZeroPool(Size
+ sizeof(CHAR8
));
1717 if (AsciiStrBuffer
== NULL
) {
1718 return EFI_OUT_OF_RESOURCES
;
1720 UscStrBuffer
= AllocateZeroPool(*BufferSize
+ sizeof(CHAR16
));
1721 if (UscStrBuffer
== NULL
) {
1722 SHELL_FREE_NON_NULL(AsciiStrBuffer
);
1723 return EFI_OUT_OF_RESOURCES
;
1725 Status
= (((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Read(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, &Size
, AsciiStrBuffer
));
1726 if (!EFI_ERROR(Status
)) {
1727 CharNum
= UnicodeSPrint(UscStrBuffer
, *BufferSize
+ sizeof(CHAR16
), L
"%a", AsciiStrBuffer
);
1728 if (CharNum
== Size
) {
1729 CopyMem (Buffer
, UscStrBuffer
, *BufferSize
);
1731 Status
= EFI_UNSUPPORTED
;
1734 SHELL_FREE_NON_NULL(AsciiStrBuffer
);
1735 SHELL_FREE_NON_NULL(UscStrBuffer
);
1741 Opens a new file relative to the source file's location.
1743 @param[in] This The protocol instance pointer.
1744 @param[out] NewHandle Returns File Handle for FileName.
1745 @param[in] FileName Null terminated string. "\", ".", and ".." are supported.
1746 @param[in] OpenMode Open mode for file.
1747 @param[in] Attributes Only used for EFI_FILE_MODE_CREATE.
1749 @retval EFI_SUCCESS The device was opened.
1750 @retval EFI_NOT_FOUND The specified file could not be found on the device.
1751 @retval EFI_NO_MEDIA The device has no media.
1752 @retval EFI_MEDIA_CHANGED The media has changed.
1753 @retval EFI_DEVICE_ERROR The device reported an error.
1754 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1755 @retval EFI_ACCESS_DENIED The service denied access to the file.
1756 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
1757 @retval EFI_VOLUME_FULL The volume is full.
1761 FileInterfaceFileOpen (
1762 IN EFI_FILE_PROTOCOL
*This
,
1763 OUT EFI_FILE_PROTOCOL
**NewHandle
,
1764 IN CHAR16
*FileName
,
1766 IN UINT64 Attributes
1769 return ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Open(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, NewHandle
, FileName
, OpenMode
, Attributes
);
1773 Close and delete the file handle.
1775 @param This Protocol instance pointer.
1777 @retval EFI_SUCCESS The device was opened.
1778 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1783 FileInterfaceFileDelete(
1784 IN EFI_FILE_PROTOCOL
*This
1788 Status
= ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Delete(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
);
1794 File style interface for File (Close).
1796 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1798 @retval EFI_SUCCESS The file was closed.
1802 FileInterfaceFileClose(
1803 IN EFI_FILE_PROTOCOL
*This
1807 Status
= ((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Close(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
);
1813 File style interface for File (Write).
1815 If the file was opened with ASCII mode the data will be processed through
1816 AsciiSPrint before writing.
1818 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.
1819 @param[in, out] BufferSize Size in bytes of Buffer.
1820 @param[in] Buffer The pointer to the buffer to write.
1822 @retval EFI_SUCCESS The data was written.
1826 FileInterfaceFileWrite(
1827 IN EFI_FILE_PROTOCOL
*This
,
1828 IN OUT UINTN
*BufferSize
,
1835 if (((EFI_FILE_PROTOCOL_FILE
*)This
)->Unicode
) {
1839 return (((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Write(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, BufferSize
, Buffer
));
1844 AsciiBuffer
= AllocateZeroPool(*BufferSize
);
1845 AsciiSPrint(AsciiBuffer
, *BufferSize
, "%S", Buffer
);
1846 Size
= AsciiStrSize(AsciiBuffer
) - 1; // (we dont need the null terminator)
1847 Status
= (((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
->Write(((EFI_FILE_PROTOCOL_FILE
*)This
)->Orig
, &Size
, AsciiBuffer
));
1848 FreePool(AsciiBuffer
);
1854 Create a file interface with unicode information.
1856 This will create a new EFI_FILE_PROTOCOL identical to the Templace
1857 except that the new one has Unicode and Ascii knowledge.
1859 @param[in] Template A pointer to the EFI_FILE_PROTOCOL object.
1860 @param[in] Unicode TRUE for UCS-2, FALSE for ASCII.
1862 @return a new EFI_FILE_PROTOCOL object to be used instead of the template.
1865 CreateFileInterfaceFile(
1866 IN CONST EFI_FILE_PROTOCOL
*Template
,
1867 IN CONST BOOLEAN Unicode
1870 EFI_FILE_PROTOCOL_FILE
*NewOne
;
1872 NewOne
= AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_FILE
));
1873 if (NewOne
== NULL
) {
1876 CopyMem(NewOne
, Template
, sizeof(EFI_FILE_PROTOCOL_FILE
));
1877 NewOne
->Orig
= (EFI_FILE_PROTOCOL
*)Template
;
1878 NewOne
->Unicode
= Unicode
;
1879 NewOne
->Open
= FileInterfaceFileOpen
;
1880 NewOne
->Close
= FileInterfaceFileClose
;
1881 NewOne
->Delete
= FileInterfaceFileDelete
;
1882 NewOne
->Read
= FileInterfaceFileRead
;
1883 NewOne
->Write
= FileInterfaceFileWrite
;
1884 NewOne
->GetPosition
= FileInterfaceFileGetPosition
;
1885 NewOne
->SetPosition
= FileInterfaceFileSetPosition
;
1886 NewOne
->GetInfo
= FileInterfaceFileGetInfo
;
1887 NewOne
->SetInfo
= FileInterfaceFileSetInfo
;
1888 NewOne
->Flush
= FileInterfaceFileFlush
;
1890 return ((EFI_FILE_PROTOCOL
*)NewOne
);