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