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*
CreateSimpleTextInOnFile(IN SHELL_FILE_HANDLE FileHandleToUse,IN EFI_HANDLE * HandleLocation)136 CreateSimpleTextInOnFile(
137 IN SHELL_FILE_HANDLE FileHandleToUse,
138 IN EFI_HANDLE *HandleLocation
139 )
140 {
141 SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ProtocolToReturn;
142 EFI_STATUS Status;
143 UINT64 CurrentPosition;
144 UINT64 FileSize;
145
146 if (HandleLocation == NULL || FileHandleToUse == NULL) {
147 return (NULL);
148 }
149
150 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL));
151 if (ProtocolToReturn == NULL) {
152 return (NULL);
153 }
154
155 ShellGetFileSize (FileHandleToUse, &FileSize);
156 ShellGetFilePosition(FileHandleToUse, &CurrentPosition);
157
158 //
159 // Initialize the protocol members
160 //
161 ProtocolToReturn->RemainingBytesOfInputFile = FileSize - CurrentPosition;
162 ProtocolToReturn->FileHandle = FileHandleToUse;
163 ProtocolToReturn->SimpleTextIn.Reset = FileBasedSimpleTextInReset;
164 ProtocolToReturn->SimpleTextIn.ReadKeyStroke = FileBasedSimpleTextInReadKeyStroke;
165
166 Status = gBS->CreateEvent (
167 EVT_NOTIFY_WAIT,
168 TPL_NOTIFY,
169 ConInWaitForKey,
170 &ProtocolToReturn->SimpleTextIn,
171 &ProtocolToReturn->SimpleTextIn.WaitForKey
172 );
173
174 if (EFI_ERROR(Status)) {
175 FreePool(ProtocolToReturn);
176 return (NULL);
177 }
178 ///@todo possibly also install SimpleTextInputEx on the handle at this point.
179 Status = gBS->InstallProtocolInterface(
180 &(ProtocolToReturn->TheHandle),
181 &gEfiSimpleTextInProtocolGuid,
182 EFI_NATIVE_INTERFACE,
183 &(ProtocolToReturn->SimpleTextIn));
184 if (!EFI_ERROR(Status)) {
185 *HandleLocation = ProtocolToReturn->TheHandle;
186 return ((EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)ProtocolToReturn);
187 } else {
188 FreePool(ProtocolToReturn);
189 return (NULL);
190 }
191 }
192
193 /**
194 Function to close a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a
195 SHELL_FILE_HANDLE to support redirecting input from a file.
196
197 @param[in] SimpleTextIn The pointer to the SimpleTextIn to close.
198
199 @retval EFI_SUCCESS The object was closed.
200 **/
201 EFI_STATUS
CloseSimpleTextInOnFile(IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL * SimpleTextIn)202 CloseSimpleTextInOnFile(
203 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextIn
204 )
205 {
206 EFI_STATUS Status;
207 EFI_STATUS Status1;
208
209 if (SimpleTextIn == NULL) {
210 return (EFI_INVALID_PARAMETER);
211 }
212
213 Status = gBS->CloseEvent(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->SimpleTextIn.WaitForKey);
214
215 Status1 = gBS->UninstallProtocolInterface(
216 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->TheHandle,
217 &gEfiSimpleTextInProtocolGuid,
218 &(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->SimpleTextIn));
219
220 FreePool(SimpleTextIn);
221 if (!EFI_ERROR(Status)) {
222 return (Status1);
223 } else {
224 return (Status);
225 }
226 }
227
228 /**
229 Reset the text output device hardware and optionaly run diagnostics.
230
231 @param This pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
232 @param ExtendedVerification Indicates that a more extensive test may be performed
233
234 @retval EFI_SUCCESS The text output device was reset.
235 **/
236 EFI_STATUS
237 EFIAPI
FileBasedSimpleTextOutReset(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN BOOLEAN ExtendedVerification)238 FileBasedSimpleTextOutReset (
239 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
240 IN BOOLEAN ExtendedVerification
241 )
242 {
243 return (EFI_SUCCESS);
244 }
245
246 /**
247 Verifies that all characters in a Unicode string can be output to the
248 target device.
249
250 @param[in] This Protocol instance pointer.
251 @param[in] WString The NULL-terminated Unicode string to be examined.
252
253 @retval EFI_SUCCESS The device(s) are capable of rendering the output string.
254 **/
255 EFI_STATUS
256 EFIAPI
FileBasedSimpleTextOutTestString(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN CHAR16 * WString)257 FileBasedSimpleTextOutTestString (
258 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
259 IN CHAR16 *WString
260 )
261 {
262 return (EFI_SUCCESS);
263 }
264
265 /**
266 Returns information for an available text mode that the output device(s)
267 supports.
268
269 @param[in] This Protocol instance pointer.
270 @param[in] ModeNumber The mode number to return information on.
271 @param[out] Columns Upon return, the number of columns in the selected geometry
272 @param[out] Rows Upon return, the number of rows in the selected geometry
273
274 @retval EFI_UNSUPPORTED The mode number was not valid.
275 **/
276 EFI_STATUS
277 EFIAPI
FileBasedSimpleTextOutQueryMode(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN ModeNumber,OUT UINTN * Columns,OUT UINTN * Rows)278 FileBasedSimpleTextOutQueryMode (
279 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
280 IN UINTN ModeNumber,
281 OUT UINTN *Columns,
282 OUT UINTN *Rows
283 )
284 {
285 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *PassThruProtocol;
286
287 PassThruProtocol = ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->OriginalSimpleTextOut;
288
289 // Pass the QueryMode call thru to the original SimpleTextOutProtocol
290 return (PassThruProtocol->QueryMode(
291 PassThruProtocol,
292 ModeNumber,
293 Columns,
294 Rows));
295 }
296
297 /**
298 Sets the output device(s) to a specified mode.
299
300 @param[in] This Protocol instance pointer.
301 @param[in] ModeNumber The mode number to set.
302
303 @retval EFI_UNSUPPORTED The mode number was not valid.
304 **/
305 EFI_STATUS
306 EFIAPI
FileBasedSimpleTextOutSetMode(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN ModeNumber)307 FileBasedSimpleTextOutSetMode (
308 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
309 IN UINTN ModeNumber
310 )
311 {
312 return (EFI_UNSUPPORTED);
313 }
314
315 /**
316 Sets the background and foreground colors for the OutputString () and
317 ClearScreen () functions.
318
319 @param[in] This Protocol instance pointer.
320 @param[in] Attribute The attribute to set. Bits 0..3 are the foreground color, and
321 bits 4..6 are the background color. All other bits are undefined
322 and must be zero. The valid Attributes are defined in this file.
323
324 @retval EFI_SUCCESS The attribute was set.
325 **/
326 EFI_STATUS
327 EFIAPI
FileBasedSimpleTextOutSetAttribute(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN Attribute)328 FileBasedSimpleTextOutSetAttribute (
329 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
330 IN UINTN Attribute
331 )
332 {
333 return (EFI_SUCCESS);
334 }
335
336 /**
337 Clears the output device(s) display to the currently selected background
338 color.
339
340 @param[in] This Protocol instance pointer.
341
342 @retval EFI_UNSUPPORTED The output device is not in a valid text mode.
343 **/
344 EFI_STATUS
345 EFIAPI
FileBasedSimpleTextOutClearScreen(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This)346 FileBasedSimpleTextOutClearScreen (
347 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
348 )
349 {
350 return (EFI_SUCCESS);
351 }
352
353 /**
354 Sets the current coordinates of the cursor position
355
356 @param[in] This Protocol instance pointer.
357 @param[in] Column Column to put the cursor in. Must be between zero and Column returned from QueryMode
358 @param[in] Row Row to put the cursor in. Must be between zero and Row returned from QueryMode
359
360 @retval EFI_SUCCESS The operation completed successfully.
361 **/
362 EFI_STATUS
363 EFIAPI
FileBasedSimpleTextOutSetCursorPosition(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN UINTN Column,IN UINTN Row)364 FileBasedSimpleTextOutSetCursorPosition (
365 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
366 IN UINTN Column,
367 IN UINTN Row
368 )
369 {
370 return (EFI_SUCCESS);
371 }
372
373 /**
374 Makes the cursor visible or invisible
375
376 @param[in] This Protocol instance pointer.
377 @param[in] Visible If TRUE, the cursor is set to be visible. If FALSE, the cursor is
378 set to be invisible.
379
380 @retval EFI_SUCCESS The operation completed successfully.
381 **/
382 EFI_STATUS
383 EFIAPI
FileBasedSimpleTextOutEnableCursor(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN BOOLEAN Visible)384 FileBasedSimpleTextOutEnableCursor (
385 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
386 IN BOOLEAN Visible
387 )
388 {
389 return (EFI_SUCCESS);
390 }
391
392 /**
393 Write a Unicode string to the output device.
394
395 @param[in] This Protocol instance pointer.
396 @param[in] WString The NULL-terminated Unicode string to be displayed on the output
397 device(s). All output devices must also support the Unicode
398 drawing defined in this file.
399 @retval EFI_SUCCESS The string was output to the device.
400 @retval EFI_DEVICE_ERROR The device reported an error while attempting to output
401 the text.
402 @retval EFI_UNSUPPORTED The output device's mode is not currently in a
403 defined text mode.
404 @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the
405 characters in the Unicode string could not be
406 rendered and were skipped.
407 **/
408 EFI_STATUS
409 EFIAPI
FileBasedSimpleTextOutOutputString(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * This,IN CHAR16 * WString)410 FileBasedSimpleTextOutOutputString (
411 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
412 IN CHAR16 *WString
413 )
414 {
415 UINTN Size;
416 Size = StrLen(WString) * sizeof(CHAR16);
417 return (ShellInfoObject.NewEfiShellProtocol->WriteFile(
418 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->FileHandle,
419 &Size,
420 WString));
421 }
422
423 /**
424 Function to create a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
425 SHELL_FILE_HANDLE to support redirecting output from a file.
426
427 @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.
428 @param[in] HandleLocation The pointer of a location to copy handle with protocol to.
429 @param[in] OriginalProtocol The pointer to the original output protocol for pass thru of functions.
430
431 @retval NULL There was insufficient memory available.
432 @return A pointer to the allocated protocol structure;
433 **/
434 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*
CreateSimpleTextOutOnFile(IN SHELL_FILE_HANDLE FileHandleToUse,IN EFI_HANDLE * HandleLocation,IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * OriginalProtocol)435 CreateSimpleTextOutOnFile(
436 IN SHELL_FILE_HANDLE FileHandleToUse,
437 IN EFI_HANDLE *HandleLocation,
438 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalProtocol
439 )
440 {
441 SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ProtocolToReturn;
442 EFI_STATUS Status;
443
444 if (HandleLocation == NULL || FileHandleToUse == NULL) {
445 return (NULL);
446 }
447
448 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL));
449 if (ProtocolToReturn == NULL) {
450 return (NULL);
451 }
452 ProtocolToReturn->FileHandle = FileHandleToUse;
453 ProtocolToReturn->OriginalSimpleTextOut = OriginalProtocol;
454 ProtocolToReturn->SimpleTextOut.Reset = FileBasedSimpleTextOutReset;
455 ProtocolToReturn->SimpleTextOut.TestString = FileBasedSimpleTextOutTestString;
456 ProtocolToReturn->SimpleTextOut.QueryMode = FileBasedSimpleTextOutQueryMode;
457 ProtocolToReturn->SimpleTextOut.SetMode = FileBasedSimpleTextOutSetMode;
458 ProtocolToReturn->SimpleTextOut.SetAttribute = FileBasedSimpleTextOutSetAttribute;
459 ProtocolToReturn->SimpleTextOut.ClearScreen = FileBasedSimpleTextOutClearScreen;
460 ProtocolToReturn->SimpleTextOut.SetCursorPosition = FileBasedSimpleTextOutSetCursorPosition;
461 ProtocolToReturn->SimpleTextOut.EnableCursor = FileBasedSimpleTextOutEnableCursor;
462 ProtocolToReturn->SimpleTextOut.OutputString = FileBasedSimpleTextOutOutputString;
463 ProtocolToReturn->SimpleTextOut.Mode = AllocateZeroPool(sizeof(EFI_SIMPLE_TEXT_OUTPUT_MODE));
464 if (ProtocolToReturn->SimpleTextOut.Mode == NULL) {
465 FreePool(ProtocolToReturn);
466 return (NULL);
467 }
468 ProtocolToReturn->SimpleTextOut.Mode->MaxMode = OriginalProtocol->Mode->MaxMode;
469 ProtocolToReturn->SimpleTextOut.Mode->Mode = OriginalProtocol->Mode->Mode;
470 ProtocolToReturn->SimpleTextOut.Mode->Attribute = OriginalProtocol->Mode->Attribute;
471 ProtocolToReturn->SimpleTextOut.Mode->CursorColumn = OriginalProtocol->Mode->CursorColumn;
472 ProtocolToReturn->SimpleTextOut.Mode->CursorRow = OriginalProtocol->Mode->CursorRow;
473 ProtocolToReturn->SimpleTextOut.Mode->CursorVisible = OriginalProtocol->Mode->CursorVisible;
474
475 Status = gBS->InstallProtocolInterface(
476 &(ProtocolToReturn->TheHandle),
477 &gEfiSimpleTextOutProtocolGuid,
478 EFI_NATIVE_INTERFACE,
479 &(ProtocolToReturn->SimpleTextOut));
480 if (!EFI_ERROR(Status)) {
481 *HandleLocation = ProtocolToReturn->TheHandle;
482 return ((EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)ProtocolToReturn);
483 } else {
484 SHELL_FREE_NON_NULL(ProtocolToReturn->SimpleTextOut.Mode);
485 SHELL_FREE_NON_NULL(ProtocolToReturn);
486 return (NULL);
487 }
488 }
489
490 /**
491 Function to close a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
492 SHELL_FILE_HANDLE to support redirecting output from a file.
493
494 @param[in] SimpleTextOut The pointer to the SimpleTextOUT to close.
495
496 @retval EFI_SUCCESS The object was closed.
497 **/
498 EFI_STATUS
CloseSimpleTextOutOnFile(IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * SimpleTextOut)499 CloseSimpleTextOutOnFile(
500 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut
501 )
502 {
503 EFI_STATUS Status;
504 if (SimpleTextOut == NULL) {
505 return (EFI_INVALID_PARAMETER);
506 }
507 Status = gBS->UninstallProtocolInterface(
508 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->TheHandle,
509 &gEfiSimpleTextOutProtocolGuid,
510 &(((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->SimpleTextOut));
511 FreePool(SimpleTextOut->Mode);
512 FreePool(SimpleTextOut);
513 return (Status);
514 }
515