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