2 Function definitions for shell simple text in and out on top of file handles.
4 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 extern BOOLEAN AsciiRedirection
;
21 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleTextIn
;
22 SHELL_FILE_HANDLE FileHandle
;
24 UINT64 RemainingBytesOfInputFile
;
25 } SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
;
28 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOut
;
29 SHELL_FILE_HANDLE FileHandle
;
31 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*OriginalSimpleTextOut
;
32 } SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
;
35 Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event
36 Signal the event if there is key available
38 @param Event Indicates the event that invoke this function.
39 @param Context Indicates the calling context.
49 gBS
->SignalEvent (Event
);
53 Reset function for the fake simple text input.
55 @param[in] This A pointer to the SimpleTextIn structure.
56 @param[in] ExtendedVerification TRUE for extra validation, FALSE otherwise.
58 @retval EFI_SUCCESS The reset was successful.
62 FileBasedSimpleTextInReset(
63 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
64 IN BOOLEAN ExtendedVerification
71 ReadKeyStroke function for the fake simple text input.
73 @param[in] This A pointer to the SimpleTextIn structure.
74 @param[in, out] Key A pointer to the Key structure to fill.
76 @retval EFI_SUCCESS The read was successful.
80 FileBasedSimpleTextInReadKeyStroke(
81 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
82 IN OUT EFI_INPUT_KEY
*Key
89 // Verify the parameters
91 if (Key
== NULL
|| This
== NULL
) {
92 return (EFI_INVALID_PARAMETER
);
96 // Check if we have any characters left in the stream.
98 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)This
)->RemainingBytesOfInputFile
== 0) {
99 return (EFI_NOT_READY
);
102 Size
= sizeof(CHAR16
);
104 if(!AsciiRedirection
) {
105 CharSize
= sizeof(CHAR16
);
107 CharSize
= sizeof(CHAR8
);
110 // Decrement the amount of free space by Size or set to zero (for odd length files)
112 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)This
)->RemainingBytesOfInputFile
> CharSize
) {
113 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)This
)->RemainingBytesOfInputFile
-= CharSize
;
115 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)This
)->RemainingBytesOfInputFile
= 0;
119 return (ShellInfoObject
.NewEfiShellProtocol
->ReadFile(
120 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)This
)->FileHandle
,
126 Function to create a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a
127 SHELL_FILE_HANDLE to support redirecting input from a file.
129 @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.
130 @param[in] HandleLocation The pointer of a location to copy handle with protocol to.
132 @retval NULL There was insufficient memory available.
133 @return A pointer to the allocated protocol structure;
135 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*
137 CreateSimpleTextInOnFile(
138 IN SHELL_FILE_HANDLE FileHandleToUse
,
139 IN EFI_HANDLE
*HandleLocation
142 SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*ProtocolToReturn
;
144 UINT64 CurrentPosition
;
147 if (HandleLocation
== NULL
|| FileHandleToUse
== NULL
) {
151 ProtocolToReturn
= AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
));
152 if (ProtocolToReturn
== NULL
) {
156 ShellGetFileSize (FileHandleToUse
, &FileSize
);
157 ShellGetFilePosition(FileHandleToUse
, &CurrentPosition
);
160 // Initialize the protocol members
162 ProtocolToReturn
->RemainingBytesOfInputFile
= FileSize
- CurrentPosition
;
163 ProtocolToReturn
->FileHandle
= FileHandleToUse
;
164 ProtocolToReturn
->SimpleTextIn
.Reset
= FileBasedSimpleTextInReset
;
165 ProtocolToReturn
->SimpleTextIn
.ReadKeyStroke
= FileBasedSimpleTextInReadKeyStroke
;
167 Status
= gBS
->CreateEvent (
171 &ProtocolToReturn
->SimpleTextIn
,
172 &ProtocolToReturn
->SimpleTextIn
.WaitForKey
175 if (EFI_ERROR(Status
)) {
176 FreePool(ProtocolToReturn
);
179 ///@todo possibly also install SimpleTextInputEx on the handle at this point.
180 Status
= gBS
->InstallProtocolInterface(
181 &(ProtocolToReturn
->TheHandle
),
182 &gEfiSimpleTextInProtocolGuid
,
183 EFI_NATIVE_INTERFACE
,
184 &(ProtocolToReturn
->SimpleTextIn
));
185 if (!EFI_ERROR(Status
)) {
186 *HandleLocation
= ProtocolToReturn
->TheHandle
;
187 return ((EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)ProtocolToReturn
);
189 FreePool(ProtocolToReturn
);
195 Function to close a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a
196 SHELL_FILE_HANDLE to support redirecting input from a file.
198 @param[in] SimpleTextIn The pointer to the SimpleTextIn to close.
200 @retval EFI_SUCCESS The object was closed.
204 CloseSimpleTextInOnFile(
205 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*SimpleTextIn
211 if (SimpleTextIn
== NULL
) {
212 return (EFI_INVALID_PARAMETER
);
215 Status
= gBS
->CloseEvent(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)SimpleTextIn
)->SimpleTextIn
.WaitForKey
);
217 Status1
= gBS
->UninstallProtocolInterface(
218 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)SimpleTextIn
)->TheHandle
,
219 &gEfiSimpleTextInProtocolGuid
,
220 &(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*)SimpleTextIn
)->SimpleTextIn
));
222 FreePool(SimpleTextIn
);
223 if (!EFI_ERROR(Status
)) {
231 Reset the text output device hardware and optionaly run diagnostics.
233 @param This pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
234 @param ExtendedVerification Indicates that a more extensive test may be performed
236 @retval EFI_SUCCESS The text output device was reset.
240 FileBasedSimpleTextOutReset (
241 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
242 IN BOOLEAN ExtendedVerification
245 return (EFI_SUCCESS
);
249 Verifies that all characters in a Unicode string can be output to the
252 @param[in] This Protocol instance pointer.
253 @param[in] WString The NULL-terminated Unicode string to be examined.
255 @retval EFI_SUCCESS The device(s) are capable of rendering the output string.
259 FileBasedSimpleTextOutTestString (
260 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
264 return (EFI_SUCCESS
);
268 Returns information for an available text mode that the output device(s)
271 @param[in] This Protocol instance pointer.
272 @param[in] ModeNumber The mode number to return information on.
273 @param[out] Columns Upon return, the number of columns in the selected geometry
274 @param[out] Rows Upon return, the number of rows in the selected geometry
276 @retval EFI_UNSUPPORTED The mode number was not valid.
280 FileBasedSimpleTextOutQueryMode (
281 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
287 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*PassThruProtocol
;
289 PassThruProtocol
= ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)This
)->OriginalSimpleTextOut
;
291 // Pass the QueryMode call thru to the original SimpleTextOutProtocol
292 return (PassThruProtocol
->QueryMode(
300 Sets the output device(s) to a specified mode.
302 @param[in] This Protocol instance pointer.
303 @param[in] ModeNumber The mode number to set.
305 @retval EFI_UNSUPPORTED The mode number was not valid.
309 FileBasedSimpleTextOutSetMode (
310 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
314 return (EFI_UNSUPPORTED
);
318 Sets the background and foreground colors for the OutputString () and
319 ClearScreen () functions.
321 @param[in] This Protocol instance pointer.
322 @param[in] Attribute The attribute to set. Bits 0..3 are the foreground color, and
323 bits 4..6 are the background color. All other bits are undefined
324 and must be zero. The valid Attributes are defined in this file.
326 @retval EFI_SUCCESS The attribute was set.
330 FileBasedSimpleTextOutSetAttribute (
331 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
335 return (EFI_SUCCESS
);
339 Clears the output device(s) display to the currently selected background
342 @param[in] This Protocol instance pointer.
344 @retval EFI_UNSUPPORTED The output device is not in a valid text mode.
348 FileBasedSimpleTextOutClearScreen (
349 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
352 return (EFI_SUCCESS
);
356 Sets the current coordinates of the cursor position
358 @param[in] This Protocol instance pointer.
359 @param[in] Column Column to put the cursor in. Must be between zero and Column returned from QueryMode
360 @param[in] Row Row to put the cursor in. Must be between zero and Row returned from QueryMode
362 @retval EFI_SUCCESS The operation completed successfully.
366 FileBasedSimpleTextOutSetCursorPosition (
367 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
372 return (EFI_SUCCESS
);
376 Makes the cursor visible or invisible
378 @param[in] This Protocol instance pointer.
379 @param[in] Visible If TRUE, the cursor is set to be visible. If FALSE, the cursor is
382 @retval EFI_SUCCESS The operation completed successfully.
386 FileBasedSimpleTextOutEnableCursor (
387 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
391 return (EFI_SUCCESS
);
395 Write a Unicode string to the output device.
397 @param[in] This Protocol instance pointer.
398 @param[in] WString The NULL-terminated Unicode string to be displayed on the output
399 device(s). All output devices must also support the Unicode
400 drawing defined in this file.
401 @retval EFI_SUCCESS The string was output to the device.
402 @retval EFI_DEVICE_ERROR The device reported an error while attempting to output
404 @retval EFI_UNSUPPORTED The output device's mode is not currently in a
406 @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the
407 characters in the Unicode string could not be
408 rendered and were skipped.
412 FileBasedSimpleTextOutOutputString (
413 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
418 Size
= StrLen(WString
) * sizeof(CHAR16
);
419 return (ShellInfoObject
.NewEfiShellProtocol
->WriteFile(
420 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)This
)->FileHandle
,
426 Function to create a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
427 SHELL_FILE_HANDLE to support redirecting output from a file.
429 @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.
430 @param[in] HandleLocation The pointer of a location to copy handle with protocol to.
431 @param[in] OriginalProtocol The pointer to the original output protocol for pass thru of functions.
433 @retval NULL There was insufficient memory available.
434 @return A pointer to the allocated protocol structure;
436 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*
438 CreateSimpleTextOutOnFile(
439 IN SHELL_FILE_HANDLE FileHandleToUse
,
440 IN EFI_HANDLE
*HandleLocation
,
441 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*OriginalProtocol
444 SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ProtocolToReturn
;
447 if (HandleLocation
== NULL
|| FileHandleToUse
== NULL
) {
451 ProtocolToReturn
= AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
));
452 if (ProtocolToReturn
== NULL
) {
455 ProtocolToReturn
->FileHandle
= FileHandleToUse
;
456 ProtocolToReturn
->OriginalSimpleTextOut
= OriginalProtocol
;
457 ProtocolToReturn
->SimpleTextOut
.Reset
= FileBasedSimpleTextOutReset
;
458 ProtocolToReturn
->SimpleTextOut
.TestString
= FileBasedSimpleTextOutTestString
;
459 ProtocolToReturn
->SimpleTextOut
.QueryMode
= FileBasedSimpleTextOutQueryMode
;
460 ProtocolToReturn
->SimpleTextOut
.SetMode
= FileBasedSimpleTextOutSetMode
;
461 ProtocolToReturn
->SimpleTextOut
.SetAttribute
= FileBasedSimpleTextOutSetAttribute
;
462 ProtocolToReturn
->SimpleTextOut
.ClearScreen
= FileBasedSimpleTextOutClearScreen
;
463 ProtocolToReturn
->SimpleTextOut
.SetCursorPosition
= FileBasedSimpleTextOutSetCursorPosition
;
464 ProtocolToReturn
->SimpleTextOut
.EnableCursor
= FileBasedSimpleTextOutEnableCursor
;
465 ProtocolToReturn
->SimpleTextOut
.OutputString
= FileBasedSimpleTextOutOutputString
;
466 ProtocolToReturn
->SimpleTextOut
.Mode
= AllocateZeroPool(sizeof(EFI_SIMPLE_TEXT_OUTPUT_MODE
));
467 if (ProtocolToReturn
->SimpleTextOut
.Mode
== NULL
) {
468 FreePool(ProtocolToReturn
);
471 ProtocolToReturn
->SimpleTextOut
.Mode
->MaxMode
= OriginalProtocol
->Mode
->MaxMode
;
472 ProtocolToReturn
->SimpleTextOut
.Mode
->Mode
= OriginalProtocol
->Mode
->Mode
;
473 ProtocolToReturn
->SimpleTextOut
.Mode
->Attribute
= OriginalProtocol
->Mode
->Attribute
;
474 ProtocolToReturn
->SimpleTextOut
.Mode
->CursorColumn
= OriginalProtocol
->Mode
->CursorColumn
;
475 ProtocolToReturn
->SimpleTextOut
.Mode
->CursorRow
= OriginalProtocol
->Mode
->CursorRow
;
476 ProtocolToReturn
->SimpleTextOut
.Mode
->CursorVisible
= OriginalProtocol
->Mode
->CursorVisible
;
478 Status
= gBS
->InstallProtocolInterface(
479 &(ProtocolToReturn
->TheHandle
),
480 &gEfiSimpleTextOutProtocolGuid
,
481 EFI_NATIVE_INTERFACE
,
482 &(ProtocolToReturn
->SimpleTextOut
));
483 if (!EFI_ERROR(Status
)) {
484 *HandleLocation
= ProtocolToReturn
->TheHandle
;
485 return ((EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)ProtocolToReturn
);
487 SHELL_FREE_NON_NULL(ProtocolToReturn
->SimpleTextOut
.Mode
);
488 SHELL_FREE_NON_NULL(ProtocolToReturn
);
494 Function to close a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
495 SHELL_FILE_HANDLE to support redirecting output from a file.
497 @param[in] SimpleTextOut The pointer to the SimpleTextOUT to close.
499 @retval EFI_SUCCESS The object was closed.
503 CloseSimpleTextOutOnFile(
504 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*SimpleTextOut
508 if (SimpleTextOut
== NULL
) {
509 return (EFI_INVALID_PARAMETER
);
511 Status
= gBS
->UninstallProtocolInterface(
512 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)SimpleTextOut
)->TheHandle
,
513 &gEfiSimpleTextOutProtocolGuid
,
514 &(((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)SimpleTextOut
)->SimpleTextOut
));
515 FreePool(SimpleTextOut
->Mode
);
516 FreePool(SimpleTextOut
);