1 /** @file
2 Function definitions for shell simple text in and out on top of file handles.
3
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
10
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.
13
14 **/
15
16 #include "Shell.h"
17
18 extern BOOLEAN AsciiRedirection;
19
20 typedef struct {
21 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleTextIn;
22 SHELL_FILE_HANDLE FileHandle;
23 EFI_HANDLE TheHandle;
24 UINT64 RemainingBytesOfInputFile;
25 } SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL;
26
27 typedef struct {
28 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOut;
29 SHELL_FILE_HANDLE FileHandle;
30 EFI_HANDLE TheHandle;
31 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalSimpleTextOut;
32 } SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;
33
34 /**
35 Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event
36 Signal the event if there is key available
37
38 @param Event Indicates the event that invoke this function.
39 @param Context Indicates the calling context.
40
41 **/
42 VOID
43 EFIAPI
ConInWaitForKey(IN EFI_EVENT Event,IN VOID * Context)44 ConInWaitForKey (
45 IN EFI_EVENT Event,
46 IN VOID *Context
47 )
48 {
49 gBS->SignalEvent (Event);
50 }
51
52 /**
53 Reset function for the fake simple text input.
54
55 @param[in] This A pointer to the SimpleTextIn structure.
56 @param[in] ExtendedVerification TRUE for extra validation, FALSE otherwise.
57
58 @retval EFI_SUCCESS The reset was successful.
59 **/
60 EFI_STATUS
61 EFIAPI
FileBasedSimpleTextInReset(IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL * This,IN BOOLEAN ExtendedVerification)62 FileBasedSimpleTextInReset(
63 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
64 IN BOOLEAN ExtendedVerification
65 )
66 {
67 return (EFI_SUCCESS);
68 }
69
70 /**
71 ReadKeyStroke function for the fake simple text input.
72
73 @param[in] This A pointer to the SimpleTextIn structure.
74 @param[in, out] Key A pointer to the Key structure to fill.
75
76 @retval EFI_SUCCESS The read was successful.
77 **/
78 EFI_STATUS
79 EFIAPI
FileBasedSimpleTextInReadKeyStroke(IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL * This,IN OUT EFI_INPUT_KEY * Key)80 FileBasedSimpleTextInReadKeyStroke(
81 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
82 IN OUT EFI_INPUT_KEY *Key
83 )
84 {
85 UINTN Size;
86 UINTN CharSize;
87
88 //
89 // Verify the parameters
90 //
91 if (Key == NULL || This == NULL) {
92 return (EFI_INVALID_PARAMETER);
93 }
94
95 //
96 // Check if we have any characters left in the stream.
97 //
98 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile == 0) {
99 return (EFI_NOT_READY);
100 }
101
102 Size = sizeof(CHAR16);
103
104 if(!AsciiRedirection) {
105 CharSize = sizeof(CHAR16);
106 } else {
107 CharSize = sizeof(CHAR8);
108 }
109 //
110 // Decrement the amount of free space by Size or set to zero (for odd length files)
111 //
112 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile > CharSize) {
113 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile -= CharSize;
114 } else {
115 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile = 0;
116 }
117
118 Key->ScanCode = 0;
119 return (ShellInfoObject.NewEfiShellProtocol->ReadFile(
120 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->FileHandle,
121 &Size,
122 &Key->UnicodeChar));
123 }
124
125 /**
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.
128
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.
131
132 @retval NULL There was insufficient memory available.
133 @return A pointer to the allocated protocol structure;
134 **/
135 EFI_SIMPLE_TEXT_INPUT_PROTOCOL*
136 EFIAPI
CreateSimpleTextInOnFile(IN SHELL_FILE_HANDLE FileHandleToUse,IN EFI_HANDLE * HandleLocation)137 CreateSimpleTextInOnFile(
138 IN SHELL_FILE_HANDLE FileHandleToUse,
139 IN EFI_HANDLE *HandleLocation
140 )
141 {
142 SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ProtocolToReturn;
143 EFI_STATUS Status;
144 UINT64 CurrentPosition;
145 UINT64 FileSize;
146
147 if (HandleLocation == NULL || FileHandleToUse == NULL) {
148 return (NULL);
149 }
150
151 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL));
152 if (ProtocolToReturn == NULL) {
153 return (NULL);
154 }
155
156 ShellGetFileSize (FileHandleToUse, &FileSize);
157 ShellGetFilePosition(FileHandleToUse, &CurrentPosition);
158
159 //
160 // Initialize the protocol members
161 //
162 ProtocolToReturn->RemainingBytesOfInputFile = FileSize - CurrentPosition;
163 ProtocolToReturn->FileHandle = FileHandleToUse;
164 ProtocolToReturn->SimpleTextIn.Reset = FileBasedSimpleTextInReset;
165 ProtocolToReturn->SimpleTextIn.ReadKeyStroke = FileBasedSimpleTextInReadKeyStroke;
166
167 Status = gBS->CreateEvent (
168 EVT_NOTIFY_WAIT,
169 TPL_NOTIFY,
170 ConInWaitForKey,
171 &ProtocolToReturn->SimpleTextIn,
172 &ProtocolToReturn->SimpleTextIn.WaitForKey
173 );
174
175 if (EFI_ERROR(Status)) {
176 FreePool(ProtocolToReturn);
177 return (NULL);
178 }
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);
188 } else {
189 FreePool(ProtocolToReturn);
190 return (NULL);
191 }
192 }
193
194 /**
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.
197
198 @param[in] SimpleTextIn The pointer to the SimpleTextIn to close.
199
200 @retval EFI_SUCCESS The object was closed.
201 **/
202 EFI_STATUS
203 EFIAPI
CloseSimpleTextInOnFile(IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL * SimpleTextIn)204 CloseSimpleTextInOnFile(
205 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextIn
206 )
207 {
208 EFI_STATUS Status;
209 EFI_STATUS Status1;
210
211 if (SimpleTextIn == NULL) {
212 return (EFI_INVALID_PARAMETER);
213 }
214
215 Status = gBS->CloseEvent(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->SimpleTextIn.WaitForKey);
216
217 Status1 = gBS->UninstallProtocolInterface(
218 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->TheHandle,
219 &gEfiSimpleTextInProtocolGuid,
220 &(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->SimpleTextIn));
221
222 FreePool(SimpleTextIn);
223 if (!EFI_ERROR(Status)) {
224 return (Status1);
225 } else {
226 return (Status);
227 }
228 }
229
230 /**
231 Reset the text output device hardware and optionaly run diagnostics.
232
233 @param This pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
234 @param ExtendedVerification Indicates that a more extensive test may be performed
235
236 @retval EFI_SUCCESS The text output device was reset.
237 **/
238 EFI_STATUS
239 EFIAPI
FileBasedSimpleTextOutReset(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN BOOLEAN ExtendedVerification)240 FileBasedSimpleTextOutReset (
241 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
242 IN BOOLEAN ExtendedVerification
243 )
244 {
245 return (EFI_SUCCESS);
246 }
247
248 /**
249 Verifies that all characters in a Unicode string can be output to the
250 target device.
251
252 @param[in] This Protocol instance pointer.
253 @param[in] WString The NULL-terminated Unicode string to be examined.
254
255 @retval EFI_SUCCESS The device(s) are capable of rendering the output string.
256 **/
257 EFI_STATUS
258 EFIAPI
FileBasedSimpleTextOutTestString(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN CHAR16 * WString)259 FileBasedSimpleTextOutTestString (
260 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
261 IN CHAR16 *WString
262 )
263 {
264 return (EFI_SUCCESS);
265 }
266
267 /**
268 Returns information for an available text mode that the output device(s)
269 supports.
270
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
275
276 @retval EFI_UNSUPPORTED The mode number was not valid.
277 **/
278 EFI_STATUS
279 EFIAPI
FileBasedSimpleTextOutQueryMode(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN ModeNumber,OUT UINTN * Columns,OUT UINTN * Rows)280 FileBasedSimpleTextOutQueryMode (
281 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
282 IN UINTN ModeNumber,
283 OUT UINTN *Columns,
284 OUT UINTN *Rows
285 )
286 {
287 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *PassThruProtocol;
288
289 PassThruProtocol = ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->OriginalSimpleTextOut;
290
291 // Pass the QueryMode call thru to the original SimpleTextOutProtocol
292 return (PassThruProtocol->QueryMode(
293 PassThruProtocol,
294 ModeNumber,
295 Columns,
296 Rows));
297 }
298
299 /**
300 Sets the output device(s) to a specified mode.
301
302 @param[in] This Protocol instance pointer.
303 @param[in] ModeNumber The mode number to set.
304
305 @retval EFI_UNSUPPORTED The mode number was not valid.
306 **/
307 EFI_STATUS
308 EFIAPI
FileBasedSimpleTextOutSetMode(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN ModeNumber)309 FileBasedSimpleTextOutSetMode (
310 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
311 IN UINTN ModeNumber
312 )
313 {
314 return (EFI_UNSUPPORTED);
315 }
316
317 /**
318 Sets the background and foreground colors for the OutputString () and
319 ClearScreen () functions.
320
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.
325
326 @retval EFI_SUCCESS The attribute was set.
327 **/
328 EFI_STATUS
329 EFIAPI
FileBasedSimpleTextOutSetAttribute(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN Attribute)330 FileBasedSimpleTextOutSetAttribute (
331 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
332 IN UINTN Attribute
333 )
334 {
335 return (EFI_SUCCESS);
336 }
337
338 /**
339 Clears the output device(s) display to the currently selected background
340 color.
341
342 @param[in] This Protocol instance pointer.
343
344 @retval EFI_UNSUPPORTED The output device is not in a valid text mode.
345 **/
346 EFI_STATUS
347 EFIAPI
FileBasedSimpleTextOutClearScreen(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This)348 FileBasedSimpleTextOutClearScreen (
349 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
350 )
351 {
352 return (EFI_SUCCESS);
353 }
354
355 /**
356 Sets the current coordinates of the cursor position
357
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
361
362 @retval EFI_SUCCESS The operation completed successfully.
363 **/
364 EFI_STATUS
365 EFIAPI
FileBasedSimpleTextOutSetCursorPosition(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN Column,IN UINTN Row)366 FileBasedSimpleTextOutSetCursorPosition (
367 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
368 IN UINTN Column,
369 IN UINTN Row
370 )
371 {
372 return (EFI_SUCCESS);
373 }
374
375 /**
376 Makes the cursor visible or invisible
377
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
380 set to be invisible.
381
382 @retval EFI_SUCCESS The operation completed successfully.
383 **/
384 EFI_STATUS
385 EFIAPI
FileBasedSimpleTextOutEnableCursor(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN BOOLEAN Visible)386 FileBasedSimpleTextOutEnableCursor (
387 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
388 IN BOOLEAN Visible
389 )
390 {
391 return (EFI_SUCCESS);
392 }
393
394 /**
395 Write a Unicode string to the output device.
396
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
403 the text.
404 @retval EFI_UNSUPPORTED The output device's mode is not currently in a
405 defined text mode.
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.
409 **/
410 EFI_STATUS
411 EFIAPI
FileBasedSimpleTextOutOutputString(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN CHAR16 * WString)412 FileBasedSimpleTextOutOutputString (
413 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
414 IN CHAR16 *WString
415 )
416 {
417 UINTN Size;
418 Size = StrLen(WString) * sizeof(CHAR16);
419 return (ShellInfoObject.NewEfiShellProtocol->WriteFile(
420 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->FileHandle,
421 &Size,
422 WString));
423 }
424
425 /**
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.
428
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.
432
433 @retval NULL There was insufficient memory available.
434 @return A pointer to the allocated protocol structure;
435 **/
436 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*
437 EFIAPI
CreateSimpleTextOutOnFile(IN SHELL_FILE_HANDLE FileHandleToUse,IN EFI_HANDLE * HandleLocation,IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * OriginalProtocol)438 CreateSimpleTextOutOnFile(
439 IN SHELL_FILE_HANDLE FileHandleToUse,
440 IN EFI_HANDLE *HandleLocation,
441 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalProtocol
442 )
443 {
444 SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ProtocolToReturn;
445 EFI_STATUS Status;
446
447 if (HandleLocation == NULL || FileHandleToUse == NULL) {
448 return (NULL);
449 }
450
451 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL));
452 if (ProtocolToReturn == NULL) {
453 return (NULL);
454 }
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);
469 return (NULL);
470 }
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;
477
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);
486 } else {
487 SHELL_FREE_NON_NULL(ProtocolToReturn->SimpleTextOut.Mode);
488 SHELL_FREE_NON_NULL(ProtocolToReturn);
489 return (NULL);
490 }
491 }
492
493 /**
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.
496
497 @param[in] SimpleTextOut The pointer to the SimpleTextOUT to close.
498
499 @retval EFI_SUCCESS The object was closed.
500 **/
501 EFI_STATUS
502 EFIAPI
CloseSimpleTextOutOnFile(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * SimpleTextOut)503 CloseSimpleTextOutOnFile(
504 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut
505 )
506 {
507 EFI_STATUS Status;
508 if (SimpleTextOut == NULL) {
509 return (EFI_INVALID_PARAMETER);
510 }
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);
517 return (Status);
518 }
519