]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Application/Shell/ConsoleWrappers.c
ShellPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / ShellPkg / Application / Shell / ConsoleWrappers.c
... / ...
CommitLineData
1/** @file\r
2 Function definitions for shell simple text in and out on top of file handles.\r
3\r
4 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include "Shell.h"\r
11\r
12extern BOOLEAN AsciiRedirection;\r
13\r
14typedef struct {\r
15 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleTextIn;\r
16 SHELL_FILE_HANDLE FileHandle;\r
17 EFI_HANDLE TheHandle;\r
18 UINT64 RemainingBytesOfInputFile;\r
19} SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL;\r
20\r
21typedef struct {\r
22 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOut;\r
23 SHELL_FILE_HANDLE FileHandle;\r
24 EFI_HANDLE TheHandle;\r
25 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalSimpleTextOut;\r
26} SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;\r
27\r
28/**\r
29 Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event\r
30 Signal the event if there is key available\r
31\r
32 @param Event Indicates the event that invoke this function.\r
33 @param Context Indicates the calling context.\r
34\r
35**/\r
36VOID\r
37EFIAPI\r
38ConInWaitForKey (\r
39 IN EFI_EVENT Event,\r
40 IN VOID *Context\r
41 )\r
42{\r
43 gBS->SignalEvent (Event);\r
44}\r
45\r
46/**\r
47 Reset function for the fake simple text input.\r
48\r
49 @param[in] This A pointer to the SimpleTextIn structure.\r
50 @param[in] ExtendedVerification TRUE for extra validation, FALSE otherwise.\r
51\r
52 @retval EFI_SUCCESS The reset was successful.\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56FileBasedSimpleTextInReset(\r
57 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
58 IN BOOLEAN ExtendedVerification\r
59 )\r
60{\r
61 return (EFI_SUCCESS);\r
62}\r
63\r
64/**\r
65 ReadKeyStroke function for the fake simple text input.\r
66\r
67 @param[in] This A pointer to the SimpleTextIn structure.\r
68 @param[in, out] Key A pointer to the Key structure to fill.\r
69\r
70 @retval EFI_SUCCESS The read was successful.\r
71**/\r
72EFI_STATUS\r
73EFIAPI\r
74FileBasedSimpleTextInReadKeyStroke(\r
75 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
76 IN OUT EFI_INPUT_KEY *Key\r
77 )\r
78{\r
79 UINTN Size;\r
80 UINTN CharSize;\r
81\r
82 //\r
83 // Verify the parameters\r
84 //\r
85 if (Key == NULL || This == NULL) {\r
86 return (EFI_INVALID_PARAMETER);\r
87 }\r
88\r
89 //\r
90 // Check if we have any characters left in the stream.\r
91 //\r
92 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile == 0) {\r
93 return (EFI_NOT_READY);\r
94 }\r
95\r
96 Size = sizeof(CHAR16);\r
97\r
98 if(!AsciiRedirection) {\r
99 CharSize = sizeof(CHAR16);\r
100 } else {\r
101 CharSize = sizeof(CHAR8);\r
102 }\r
103 //\r
104 // Decrement the amount of free space by Size or set to zero (for odd length files)\r
105 //\r
106 if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile > CharSize) {\r
107 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile -= CharSize;\r
108 } else {\r
109 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile = 0;\r
110 }\r
111\r
112 Key->ScanCode = 0;\r
113 return (ShellInfoObject.NewEfiShellProtocol->ReadFile(\r
114 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->FileHandle,\r
115 &Size,\r
116 &Key->UnicodeChar));\r
117}\r
118\r
119/**\r
120 Function to create a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a\r
121 SHELL_FILE_HANDLE to support redirecting input from a file.\r
122\r
123 @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.\r
124 @param[in] HandleLocation The pointer of a location to copy handle with protocol to.\r
125\r
126 @retval NULL There was insufficient memory available.\r
127 @return A pointer to the allocated protocol structure;\r
128**/\r
129EFI_SIMPLE_TEXT_INPUT_PROTOCOL*\r
130CreateSimpleTextInOnFile(\r
131 IN SHELL_FILE_HANDLE FileHandleToUse,\r
132 IN EFI_HANDLE *HandleLocation\r
133 )\r
134{\r
135 SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ProtocolToReturn;\r
136 EFI_STATUS Status;\r
137 UINT64 CurrentPosition;\r
138 UINT64 FileSize;\r
139\r
140 if (HandleLocation == NULL || FileHandleToUse == NULL) {\r
141 return (NULL);\r
142 }\r
143\r
144 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL));\r
145 if (ProtocolToReturn == NULL) {\r
146 return (NULL);\r
147 }\r
148\r
149 ShellGetFileSize (FileHandleToUse, &FileSize);\r
150 ShellGetFilePosition(FileHandleToUse, &CurrentPosition);\r
151\r
152 //\r
153 // Initialize the protocol members\r
154 //\r
155 ProtocolToReturn->RemainingBytesOfInputFile = FileSize - CurrentPosition;\r
156 ProtocolToReturn->FileHandle = FileHandleToUse;\r
157 ProtocolToReturn->SimpleTextIn.Reset = FileBasedSimpleTextInReset;\r
158 ProtocolToReturn->SimpleTextIn.ReadKeyStroke = FileBasedSimpleTextInReadKeyStroke;\r
159\r
160 Status = gBS->CreateEvent (\r
161 EVT_NOTIFY_WAIT,\r
162 TPL_NOTIFY,\r
163 ConInWaitForKey,\r
164 &ProtocolToReturn->SimpleTextIn,\r
165 &ProtocolToReturn->SimpleTextIn.WaitForKey\r
166 );\r
167\r
168 if (EFI_ERROR(Status)) {\r
169 FreePool(ProtocolToReturn);\r
170 return (NULL);\r
171 }\r
172 ///@todo possibly also install SimpleTextInputEx on the handle at this point.\r
173 Status = gBS->InstallProtocolInterface(\r
174 &(ProtocolToReturn->TheHandle),\r
175 &gEfiSimpleTextInProtocolGuid,\r
176 EFI_NATIVE_INTERFACE,\r
177 &(ProtocolToReturn->SimpleTextIn));\r
178 if (!EFI_ERROR(Status)) {\r
179 *HandleLocation = ProtocolToReturn->TheHandle;\r
180 return ((EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)ProtocolToReturn);\r
181 } else {\r
182 FreePool(ProtocolToReturn);\r
183 return (NULL);\r
184 }\r
185}\r
186\r
187/**\r
188 Function to close a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a\r
189 SHELL_FILE_HANDLE to support redirecting input from a file.\r
190\r
191 @param[in] SimpleTextIn The pointer to the SimpleTextIn to close.\r
192\r
193 @retval EFI_SUCCESS The object was closed.\r
194**/\r
195EFI_STATUS\r
196CloseSimpleTextInOnFile(\r
197 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextIn\r
198 )\r
199{\r
200 EFI_STATUS Status;\r
201 EFI_STATUS Status1;\r
202\r
203 if (SimpleTextIn == NULL) {\r
204 return (EFI_INVALID_PARAMETER);\r
205 }\r
206\r
207 Status = gBS->CloseEvent(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->SimpleTextIn.WaitForKey);\r
208\r
209 Status1 = gBS->UninstallProtocolInterface(\r
210 ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->TheHandle,\r
211 &gEfiSimpleTextInProtocolGuid,\r
212 &(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL*)SimpleTextIn)->SimpleTextIn));\r
213\r
214 FreePool(SimpleTextIn);\r
215 if (!EFI_ERROR(Status)) {\r
216 return (Status1);\r
217 } else {\r
218 return (Status);\r
219 }\r
220}\r
221\r
222/**\r
223 Reset the text output device hardware and optionaly run diagnostics.\r
224\r
225 @param This pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL\r
226 @param ExtendedVerification Indicates that a more extensive test may be performed\r
227\r
228 @retval EFI_SUCCESS The text output device was reset.\r
229**/\r
230EFI_STATUS\r
231EFIAPI\r
232FileBasedSimpleTextOutReset (\r
233 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
234 IN BOOLEAN ExtendedVerification\r
235 )\r
236{\r
237 return (EFI_SUCCESS);\r
238}\r
239\r
240/**\r
241 Verifies that all characters in a Unicode string can be output to the\r
242 target device.\r
243\r
244 @param[in] This Protocol instance pointer.\r
245 @param[in] WString The NULL-terminated Unicode string to be examined.\r
246\r
247 @retval EFI_SUCCESS The device(s) are capable of rendering the output string.\r
248**/\r
249EFI_STATUS\r
250EFIAPI\r
251FileBasedSimpleTextOutTestString (\r
252 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
253 IN CHAR16 *WString\r
254 )\r
255{\r
256 return (EFI_SUCCESS);\r
257}\r
258\r
259/**\r
260 Returns information for an available text mode that the output device(s)\r
261 supports.\r
262\r
263 @param[in] This Protocol instance pointer.\r
264 @param[in] ModeNumber The mode number to return information on.\r
265 @param[out] Columns Upon return, the number of columns in the selected geometry\r
266 @param[out] Rows Upon return, the number of rows in the selected geometry\r
267\r
268 @retval EFI_UNSUPPORTED The mode number was not valid.\r
269**/\r
270EFI_STATUS\r
271EFIAPI\r
272FileBasedSimpleTextOutQueryMode (\r
273 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
274 IN UINTN ModeNumber,\r
275 OUT UINTN *Columns,\r
276 OUT UINTN *Rows\r
277 )\r
278{\r
279 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *PassThruProtocol;\r
280\r
281 PassThruProtocol = ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->OriginalSimpleTextOut;\r
282\r
283 // Pass the QueryMode call thru to the original SimpleTextOutProtocol\r
284 return (PassThruProtocol->QueryMode(\r
285 PassThruProtocol,\r
286 ModeNumber,\r
287 Columns,\r
288 Rows));\r
289}\r
290\r
291/**\r
292 Sets the output device(s) to a specified mode.\r
293\r
294 @param[in] This Protocol instance pointer.\r
295 @param[in] ModeNumber The mode number to set.\r
296\r
297 @retval EFI_UNSUPPORTED The mode number was not valid.\r
298**/\r
299EFI_STATUS\r
300EFIAPI\r
301FileBasedSimpleTextOutSetMode (\r
302 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
303 IN UINTN ModeNumber\r
304 )\r
305{\r
306 return (EFI_UNSUPPORTED);\r
307}\r
308\r
309/**\r
310 Sets the background and foreground colors for the OutputString () and\r
311 ClearScreen () functions.\r
312\r
313 @param[in] This Protocol instance pointer.\r
314 @param[in] Attribute The attribute to set. Bits 0..3 are the foreground color, and\r
315 bits 4..6 are the background color. All other bits are undefined\r
316 and must be zero. The valid Attributes are defined in this file.\r
317\r
318 @retval EFI_SUCCESS The attribute was set.\r
319**/\r
320EFI_STATUS\r
321EFIAPI\r
322FileBasedSimpleTextOutSetAttribute (\r
323 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
324 IN UINTN Attribute\r
325 )\r
326{\r
327 return (EFI_SUCCESS);\r
328}\r
329\r
330/**\r
331 Clears the output device(s) display to the currently selected background\r
332 color.\r
333\r
334 @param[in] This Protocol instance pointer.\r
335\r
336 @retval EFI_UNSUPPORTED The output device is not in a valid text mode.\r
337**/\r
338EFI_STATUS\r
339EFIAPI\r
340FileBasedSimpleTextOutClearScreen (\r
341 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This\r
342 )\r
343{\r
344 return (EFI_SUCCESS);\r
345}\r
346\r
347/**\r
348 Sets the current coordinates of the cursor position\r
349\r
350 @param[in] This Protocol instance pointer.\r
351 @param[in] Column Column to put the cursor in. Must be between zero and Column returned from QueryMode\r
352 @param[in] Row Row to put the cursor in. Must be between zero and Row returned from QueryMode\r
353\r
354 @retval EFI_SUCCESS The operation completed successfully.\r
355**/\r
356EFI_STATUS\r
357EFIAPI\r
358FileBasedSimpleTextOutSetCursorPosition (\r
359 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
360 IN UINTN Column,\r
361 IN UINTN Row\r
362 )\r
363{\r
364 return (EFI_SUCCESS);\r
365}\r
366\r
367/**\r
368 Makes the cursor visible or invisible\r
369\r
370 @param[in] This Protocol instance pointer.\r
371 @param[in] Visible If TRUE, the cursor is set to be visible. If FALSE, the cursor is\r
372 set to be invisible.\r
373\r
374 @retval EFI_SUCCESS The operation completed successfully.\r
375**/\r
376EFI_STATUS\r
377EFIAPI\r
378FileBasedSimpleTextOutEnableCursor (\r
379 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
380 IN BOOLEAN Visible\r
381 )\r
382{\r
383 return (EFI_SUCCESS);\r
384}\r
385\r
386/**\r
387 Write a Unicode string to the output device.\r
388\r
389 @param[in] This Protocol instance pointer.\r
390 @param[in] WString The NULL-terminated Unicode string to be displayed on the output\r
391 device(s). All output devices must also support the Unicode\r
392 drawing defined in this file.\r
393 @retval EFI_SUCCESS The string was output to the device.\r
394 @retval EFI_DEVICE_ERROR The device reported an error while attempting to output\r
395 the text.\r
396 @retval EFI_UNSUPPORTED The output device's mode is not currently in a\r
397 defined text mode.\r
398 @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the\r
399 characters in the Unicode string could not be\r
400 rendered and were skipped.\r
401**/\r
402EFI_STATUS\r
403EFIAPI\r
404FileBasedSimpleTextOutOutputString (\r
405 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,\r
406 IN CHAR16 *WString\r
407 )\r
408{\r
409 UINTN Size;\r
410 Size = StrLen(WString) * sizeof(CHAR16);\r
411 return (ShellInfoObject.NewEfiShellProtocol->WriteFile(\r
412 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->FileHandle,\r
413 &Size,\r
414 WString));\r
415}\r
416\r
417/**\r
418 Function to create a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a\r
419 SHELL_FILE_HANDLE to support redirecting output from a file.\r
420\r
421 @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.\r
422 @param[in] HandleLocation The pointer of a location to copy handle with protocol to.\r
423 @param[in] OriginalProtocol The pointer to the original output protocol for pass thru of functions.\r
424\r
425 @retval NULL There was insufficient memory available.\r
426 @return A pointer to the allocated protocol structure;\r
427**/\r
428EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*\r
429CreateSimpleTextOutOnFile(\r
430 IN SHELL_FILE_HANDLE FileHandleToUse,\r
431 IN EFI_HANDLE *HandleLocation,\r
432 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalProtocol\r
433 )\r
434{\r
435 SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ProtocolToReturn;\r
436 EFI_STATUS Status;\r
437\r
438 if (HandleLocation == NULL || FileHandleToUse == NULL) {\r
439 return (NULL);\r
440 }\r
441\r
442 ProtocolToReturn = AllocateZeroPool(sizeof(SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL));\r
443 if (ProtocolToReturn == NULL) {\r
444 return (NULL);\r
445 }\r
446 ProtocolToReturn->FileHandle = FileHandleToUse;\r
447 ProtocolToReturn->OriginalSimpleTextOut = OriginalProtocol;\r
448 ProtocolToReturn->SimpleTextOut.Reset = FileBasedSimpleTextOutReset;\r
449 ProtocolToReturn->SimpleTextOut.TestString = FileBasedSimpleTextOutTestString;\r
450 ProtocolToReturn->SimpleTextOut.QueryMode = FileBasedSimpleTextOutQueryMode;\r
451 ProtocolToReturn->SimpleTextOut.SetMode = FileBasedSimpleTextOutSetMode;\r
452 ProtocolToReturn->SimpleTextOut.SetAttribute = FileBasedSimpleTextOutSetAttribute;\r
453 ProtocolToReturn->SimpleTextOut.ClearScreen = FileBasedSimpleTextOutClearScreen;\r
454 ProtocolToReturn->SimpleTextOut.SetCursorPosition = FileBasedSimpleTextOutSetCursorPosition;\r
455 ProtocolToReturn->SimpleTextOut.EnableCursor = FileBasedSimpleTextOutEnableCursor;\r
456 ProtocolToReturn->SimpleTextOut.OutputString = FileBasedSimpleTextOutOutputString;\r
457 ProtocolToReturn->SimpleTextOut.Mode = AllocateZeroPool(sizeof(EFI_SIMPLE_TEXT_OUTPUT_MODE));\r
458 if (ProtocolToReturn->SimpleTextOut.Mode == NULL) {\r
459 FreePool(ProtocolToReturn);\r
460 return (NULL);\r
461 }\r
462 ProtocolToReturn->SimpleTextOut.Mode->MaxMode = OriginalProtocol->Mode->MaxMode;\r
463 ProtocolToReturn->SimpleTextOut.Mode->Mode = OriginalProtocol->Mode->Mode;\r
464 ProtocolToReturn->SimpleTextOut.Mode->Attribute = OriginalProtocol->Mode->Attribute;\r
465 ProtocolToReturn->SimpleTextOut.Mode->CursorColumn = OriginalProtocol->Mode->CursorColumn;\r
466 ProtocolToReturn->SimpleTextOut.Mode->CursorRow = OriginalProtocol->Mode->CursorRow;\r
467 ProtocolToReturn->SimpleTextOut.Mode->CursorVisible = OriginalProtocol->Mode->CursorVisible;\r
468\r
469 Status = gBS->InstallProtocolInterface(\r
470 &(ProtocolToReturn->TheHandle),\r
471 &gEfiSimpleTextOutProtocolGuid,\r
472 EFI_NATIVE_INTERFACE,\r
473 &(ProtocolToReturn->SimpleTextOut));\r
474 if (!EFI_ERROR(Status)) {\r
475 *HandleLocation = ProtocolToReturn->TheHandle;\r
476 return ((EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)ProtocolToReturn);\r
477 } else {\r
478 SHELL_FREE_NON_NULL(ProtocolToReturn->SimpleTextOut.Mode);\r
479 SHELL_FREE_NON_NULL(ProtocolToReturn);\r
480 return (NULL);\r
481 }\r
482}\r
483\r
484/**\r
485 Function to close a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a\r
486 SHELL_FILE_HANDLE to support redirecting output from a file.\r
487\r
488 @param[in] SimpleTextOut The pointer to the SimpleTextOUT to close.\r
489\r
490 @retval EFI_SUCCESS The object was closed.\r
491**/\r
492EFI_STATUS\r
493CloseSimpleTextOutOnFile(\r
494 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut\r
495 )\r
496{\r
497 EFI_STATUS Status;\r
498 if (SimpleTextOut == NULL) {\r
499 return (EFI_INVALID_PARAMETER);\r
500 }\r
501 Status = gBS->UninstallProtocolInterface(\r
502 ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->TheHandle,\r
503 &gEfiSimpleTextOutProtocolGuid,\r
504 &(((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->SimpleTextOut));\r
505 FreePool(SimpleTextOut->Mode);\r
506 FreePool(SimpleTextOut);\r
507 return (Status);\r
508}\r