]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/GraphicsConsoleDxe/GraphicsConsole.c
MdeModulePkg GraphicsConsole: Add text mode for PcdConOutColumn/Row
[mirror_edk2.git] / MdeModulePkg / Universal / Console / GraphicsConsoleDxe / GraphicsConsole.c
1 /** @file
2 This is the main routine for initializing the Graphics Console support routines.
3
4 Copyright (c) 2006 - 2010, 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 "GraphicsConsole.h"
16
17 //
18 // Graphics Console Device Private Data template
19 //
20 GRAPHICS_CONSOLE_DEV mGraphicsConsoleDevTemplate = {
21 GRAPHICS_CONSOLE_DEV_SIGNATURE,
22 (EFI_GRAPHICS_OUTPUT_PROTOCOL *) NULL,
23 (EFI_UGA_DRAW_PROTOCOL *) NULL,
24 {
25 GraphicsConsoleConOutReset,
26 GraphicsConsoleConOutOutputString,
27 GraphicsConsoleConOutTestString,
28 GraphicsConsoleConOutQueryMode,
29 GraphicsConsoleConOutSetMode,
30 GraphicsConsoleConOutSetAttribute,
31 GraphicsConsoleConOutClearScreen,
32 GraphicsConsoleConOutSetCursorPosition,
33 GraphicsConsoleConOutEnableCursor,
34 (EFI_SIMPLE_TEXT_OUTPUT_MODE *) NULL
35 },
36 {
37 0,
38 0,
39 EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_BLACK),
40 0,
41 0,
42 TRUE
43 },
44 {
45 { 80, 25, 0, 0, 0, 0, 0 }, // Mode 0
46 { 80, 50, 0, 0, 0, 0, 0 }, // Mode 1
47 { 100,31, 0, 0, 0, 0, 0 }, // Mode 2
48 { 0, 0, 0, 0, 0, 0, 0 }, // Mode 3
49 { 0, 0, 0, 0, 0, 0, 0 } // Mode 4
50 },
51 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL
52 };
53
54 EFI_HII_DATABASE_PROTOCOL *mHiiDatabase;
55 EFI_HII_FONT_PROTOCOL *mHiiFont;
56 EFI_HII_HANDLE mHiiHandle;
57 EFI_EVENT mHiiRegistration;
58
59 EFI_GUID mFontPackageListGuid = {0xf5f219d3, 0x7006, 0x4648, {0xac, 0x8d, 0xd6, 0x1d, 0xfb, 0x7b, 0xc6, 0xad}};
60
61 CHAR16 mCrLfString[3] = { CHAR_CARRIAGE_RETURN, CHAR_LINEFEED, CHAR_NULL };
62
63 EFI_GRAPHICS_OUTPUT_BLT_PIXEL mGraphicsEfiColors[16] = {
64 //
65 // B G R reserved
66 //
67 {0x00, 0x00, 0x00, 0x00}, // BLACK
68 {0x98, 0x00, 0x00, 0x00}, // LIGHTBLUE
69 {0x00, 0x98, 0x00, 0x00}, // LIGHGREEN
70 {0x98, 0x98, 0x00, 0x00}, // LIGHCYAN
71 {0x00, 0x00, 0x98, 0x00}, // LIGHRED
72 {0x98, 0x00, 0x98, 0x00}, // MAGENTA
73 {0x00, 0x98, 0x98, 0x00}, // BROWN
74 {0x98, 0x98, 0x98, 0x00}, // LIGHTGRAY
75 {0x30, 0x30, 0x30, 0x00}, // DARKGRAY - BRIGHT BLACK
76 {0xff, 0x00, 0x00, 0x00}, // BLUE
77 {0x00, 0xff, 0x00, 0x00}, // LIME
78 {0xff, 0xff, 0x00, 0x00}, // CYAN
79 {0x00, 0x00, 0xff, 0x00}, // RED
80 {0xff, 0x00, 0xff, 0x00}, // FUCHSIA
81 {0x00, 0xff, 0xff, 0x00}, // YELLOW
82 {0xff, 0xff, 0xff, 0x00} // WHITE
83 };
84
85 EFI_NARROW_GLYPH mCursorGlyph = {
86 0x0000,
87 0x00,
88 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF }
89 };
90
91 CHAR16 SpaceStr[] = { NARROW_CHAR, ' ', 0 };
92
93 EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {
94 GraphicsConsoleControllerDriverSupported,
95 GraphicsConsoleControllerDriverStart,
96 GraphicsConsoleControllerDriverStop,
97 0xa,
98 NULL,
99 NULL
100 };
101
102 /**
103 Test to see if Graphics Console could be supported on the Controller.
104
105 Graphics Console could be supported if Graphics Output Protocol or UGA Draw
106 Protocol exists on the Controller. (UGA Draw Protocol could be skipped
107 if PcdUgaConsumeSupport is set to FALSE.)
108
109 @param This Protocol instance pointer.
110 @param Controller Handle of device to test.
111 @param RemainingDevicePath Optional parameter use to pick a specific child
112 device to start.
113
114 @retval EFI_SUCCESS This driver supports this device.
115 @retval other This driver does not support this device.
116
117 **/
118 EFI_STATUS
119 EFIAPI
120 GraphicsConsoleControllerDriverSupported (
121 IN EFI_DRIVER_BINDING_PROTOCOL *This,
122 IN EFI_HANDLE Controller,
123 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
124 )
125 {
126 EFI_STATUS Status;
127 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
128 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
129 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
130
131 GraphicsOutput = NULL;
132 UgaDraw = NULL;
133 //
134 // Open the IO Abstraction(s) needed to perform the supported test
135 //
136 Status = gBS->OpenProtocol (
137 Controller,
138 &gEfiGraphicsOutputProtocolGuid,
139 (VOID **) &GraphicsOutput,
140 This->DriverBindingHandle,
141 Controller,
142 EFI_OPEN_PROTOCOL_BY_DRIVER
143 );
144
145 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
146 //
147 // Open Graphics Output Protocol failed, try to open UGA Draw Protocol
148 //
149 Status = gBS->OpenProtocol (
150 Controller,
151 &gEfiUgaDrawProtocolGuid,
152 (VOID **) &UgaDraw,
153 This->DriverBindingHandle,
154 Controller,
155 EFI_OPEN_PROTOCOL_BY_DRIVER
156 );
157 }
158 if (EFI_ERROR (Status)) {
159 return Status;
160 }
161
162 //
163 // We need to ensure that we do not layer on top of a virtual handle.
164 // We need to ensure that the handles produced by the conspliter do not
165 // get used.
166 //
167 Status = gBS->OpenProtocol (
168 Controller,
169 &gEfiDevicePathProtocolGuid,
170 (VOID **) &DevicePath,
171 This->DriverBindingHandle,
172 Controller,
173 EFI_OPEN_PROTOCOL_BY_DRIVER
174 );
175 if (!EFI_ERROR (Status)) {
176 gBS->CloseProtocol (
177 Controller,
178 &gEfiDevicePathProtocolGuid,
179 This->DriverBindingHandle,
180 Controller
181 );
182 } else {
183 goto Error;
184 }
185
186 //
187 // Does Hii Exist? If not, we aren't ready to run
188 //
189 Status = EfiLocateHiiProtocol ();
190
191 //
192 // Close the I/O Abstraction(s) used to perform the supported test
193 //
194 Error:
195 if (GraphicsOutput != NULL) {
196 gBS->CloseProtocol (
197 Controller,
198 &gEfiGraphicsOutputProtocolGuid,
199 This->DriverBindingHandle,
200 Controller
201 );
202 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
203 gBS->CloseProtocol (
204 Controller,
205 &gEfiUgaDrawProtocolGuid,
206 This->DriverBindingHandle,
207 Controller
208 );
209 }
210 return Status;
211 }
212
213
214 /**
215 Start this driver on Controller by opening Graphics Output protocol or
216 UGA Draw protocol, and installing Simple Text Out protocol on Controller.
217 (UGA Draw protocol could be skipped if PcdUgaConsumeSupport is set to FALSE.)
218
219 @param This Protocol instance pointer.
220 @param Controller Handle of device to bind driver to
221 @param RemainingDevicePath Optional parameter use to pick a specific child
222 device to start.
223
224 @retval EFI_SUCCESS This driver is added to Controller.
225 @retval other This driver does not support this device.
226
227 **/
228 EFI_STATUS
229 EFIAPI
230 GraphicsConsoleControllerDriverStart (
231 IN EFI_DRIVER_BINDING_PROTOCOL *This,
232 IN EFI_HANDLE Controller,
233 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
234 )
235 {
236 EFI_STATUS Status;
237 GRAPHICS_CONSOLE_DEV *Private;
238 UINT32 HorizontalResolution;
239 UINT32 VerticalResolution;
240 UINT32 ColorDepth;
241 UINT32 RefreshRate;
242 UINTN ModeIndex;
243 UINTN MaxMode;
244 UINTN Columns;
245 UINTN Rows;
246 UINT32 ModeNumber;
247 EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *Mode;
248 GRAPHICS_CONSOLE_MODE_DATA *ModeData;
249 ModeNumber = 0;
250
251 //
252 // Initialize the Graphics Console device instance
253 //
254 Private = AllocateCopyPool (
255 sizeof (GRAPHICS_CONSOLE_DEV),
256 &mGraphicsConsoleDevTemplate
257 );
258 if (Private == NULL) {
259 return EFI_OUT_OF_RESOURCES;
260 }
261
262 Private->SimpleTextOutput.Mode = &(Private->SimpleTextOutputMode);
263
264 Status = gBS->OpenProtocol (
265 Controller,
266 &gEfiGraphicsOutputProtocolGuid,
267 (VOID **) &Private->GraphicsOutput,
268 This->DriverBindingHandle,
269 Controller,
270 EFI_OPEN_PROTOCOL_BY_DRIVER
271 );
272
273 if (EFI_ERROR(Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
274 Status = gBS->OpenProtocol (
275 Controller,
276 &gEfiUgaDrawProtocolGuid,
277 (VOID **) &Private->UgaDraw,
278 This->DriverBindingHandle,
279 Controller,
280 EFI_OPEN_PROTOCOL_BY_DRIVER
281 );
282 }
283
284 if (EFI_ERROR (Status)) {
285 goto Error;
286 }
287
288 //
289 // If the current mode information can not be retrieved, then attempt to set the default mode
290 // of 800x600, 32 bit color, 60 Hz refresh.
291 //
292 HorizontalResolution = 800;
293 VerticalResolution = 600;
294
295 if (Private->GraphicsOutput != NULL) {
296 //
297 // The console is build on top of Graphics Output Protocol, find the mode number
298 // for the user-defined mode; if there are multiple video devices,
299 // graphic console driver will set all the video devices to the same mode.
300 //
301 Status = CheckModeSupported (
302 Private->GraphicsOutput,
303 CURRENT_HORIZONTAL_RESOLUTION,
304 CURRENT_VERTICAL_RESOLUTION,
305 &ModeNumber
306 );
307 if (!EFI_ERROR(Status)) {
308 //
309 // Update default mode to current mode
310 //
311 HorizontalResolution = CURRENT_HORIZONTAL_RESOLUTION;
312 VerticalResolution = CURRENT_VERTICAL_RESOLUTION;
313 } else {
314 //
315 // if not supporting current mode, try 800x600 which is required by UEFI/EFI spec
316 //
317 Status = CheckModeSupported (
318 Private->GraphicsOutput,
319 800,
320 600,
321 &ModeNumber
322 );
323 }
324
325 Mode = Private->GraphicsOutput->Mode;
326
327 if (EFI_ERROR (Status) || (Mode->MaxMode != 0)) {
328 //
329 // Set default mode failed or device don't support default mode, then get the current mode information
330 //
331 HorizontalResolution = Mode->Info->HorizontalResolution;
332 VerticalResolution = Mode->Info->VerticalResolution;
333 ModeNumber = Mode->Mode;
334 }
335 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
336 //
337 // At first try to set user-defined resolution
338 //
339 ColorDepth = 32;
340 RefreshRate = 60;
341 Status = Private->UgaDraw->SetMode (
342 Private->UgaDraw,
343 CURRENT_HORIZONTAL_RESOLUTION,
344 CURRENT_VERTICAL_RESOLUTION,
345 ColorDepth,
346 RefreshRate
347 );
348 if (!EFI_ERROR (Status)) {
349 HorizontalResolution = CURRENT_HORIZONTAL_RESOLUTION;
350 VerticalResolution = CURRENT_VERTICAL_RESOLUTION;
351 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
352 //
353 // Try to set 800*600 which is required by UEFI/EFI spec
354 //
355 Status = Private->UgaDraw->SetMode (
356 Private->UgaDraw,
357 HorizontalResolution,
358 VerticalResolution,
359 ColorDepth,
360 RefreshRate
361 );
362 if (EFI_ERROR (Status)) {
363 Status = Private->UgaDraw->GetMode (
364 Private->UgaDraw,
365 &HorizontalResolution,
366 &VerticalResolution,
367 &ColorDepth,
368 &RefreshRate
369 );
370 if (EFI_ERROR (Status)) {
371 goto Error;
372 }
373 }
374 } else {
375 Status = EFI_UNSUPPORTED;
376 goto Error;
377 }
378 }
379
380 //
381 // Add Mode #3 that uses the entire display for user-defined mode
382 //
383 Private->ModeData[3].Columns = HorizontalResolution / EFI_GLYPH_WIDTH;
384 Private->ModeData[3].Rows = VerticalResolution / EFI_GLYPH_HEIGHT;
385
386 //
387 // Add Mode #4 that uses the PCD values
388 //
389 Private->ModeData[4].Columns = (UINTN) PcdGet32 (PcdConOutColumn);
390 Private->ModeData[4].Rows = (UINTN) PcdGet32 (PcdConOutRow);
391
392 //
393 // Compute the maximum number of text Rows and Columns that this current graphics mode can support
394 //
395 Columns = HorizontalResolution / EFI_GLYPH_WIDTH;
396 Rows = VerticalResolution / EFI_GLYPH_HEIGHT;
397
398 //
399 // Here we make sure that mode 0 is valid
400 //
401 if (Columns < Private->ModeData[0].Columns ||
402 Rows < Private->ModeData[0].Rows) {
403 //
404 // 80x25 cannot be supported.
405 //
406 // Fallback to using the PcdConOutColumn and PcdConOutRow
407 // for mode 0. If the PCDs are also to large, then mode 0
408 // will be shrunk to fit as needed.
409 //
410 Private->ModeData[0].Columns = MIN (Private->ModeData[4].Columns, Columns);
411 Private->ModeData[0].Rows = MIN (Private->ModeData[4].Rows, Rows);
412 }
413
414 MaxMode = 0;
415 for (ModeIndex = 0; ModeIndex < GRAPHICS_MAX_MODE; ModeIndex++) {
416 ModeData = &Private->ModeData[ModeIndex];
417 ModeData->GopWidth = HorizontalResolution;
418 ModeData->GopHeight = VerticalResolution;
419 ModeData->GopModeNumber = ModeNumber;
420 if (Columns >= ModeData->Columns &&
421 Rows >= ModeData->Rows) {
422 ModeData->DeltaX = (HorizontalResolution - (ModeData->Columns * EFI_GLYPH_WIDTH)) >> 1;
423 ModeData->DeltaY = (VerticalResolution - (ModeData->Rows * EFI_GLYPH_HEIGHT)) >> 1;
424 MaxMode = ModeIndex + 1;
425 } else {
426 ModeData->Columns = 0;
427 ModeData->Rows = 0;
428 ModeData->DeltaX = 0;
429 ModeData->DeltaY = 0;
430 }
431 }
432
433 //
434 // See if the resolution was too small to support any text modes
435 //
436 if (MaxMode == 0) {
437 goto Error;
438 }
439
440 //
441 // Update the maximum number of modes
442 //
443 Private->SimpleTextOutputMode.MaxMode = (INT32) MaxMode;
444
445 //
446 // Determine the number of text modes that this protocol can support
447 //
448 Status = GraphicsConsoleConOutSetMode (&Private->SimpleTextOutput, 0);
449 if (EFI_ERROR (Status)) {
450 goto Error;
451 }
452
453 DEBUG_CODE_BEGIN ();
454 GraphicsConsoleConOutOutputString (&Private->SimpleTextOutput, (CHAR16 *)L"Graphics Console Started\n\r");
455 DEBUG_CODE_END ();
456
457 //
458 // Install protocol interfaces for the Graphics Console device.
459 //
460 Status = gBS->InstallMultipleProtocolInterfaces (
461 &Controller,
462 &gEfiSimpleTextOutProtocolGuid,
463 &Private->SimpleTextOutput,
464 NULL
465 );
466
467 Error:
468 if (EFI_ERROR (Status)) {
469 //
470 // Close the GOP and UGA Draw Protocol
471 //
472 if (Private->GraphicsOutput != NULL) {
473 gBS->CloseProtocol (
474 Controller,
475 &gEfiGraphicsOutputProtocolGuid,
476 This->DriverBindingHandle,
477 Controller
478 );
479 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
480 gBS->CloseProtocol (
481 Controller,
482 &gEfiUgaDrawProtocolGuid,
483 This->DriverBindingHandle,
484 Controller
485 );
486 }
487
488 if (Private->LineBuffer != NULL) {
489 FreePool (Private->LineBuffer);
490 }
491
492 //
493 // Free private data
494 //
495 FreePool (Private);
496 }
497
498 return Status;
499 }
500
501 /**
502 Stop this driver on Controller by removing Simple Text Out protocol
503 and closing the Graphics Output Protocol or UGA Draw protocol on Controller.
504 (UGA Draw protocol could be skipped if PcdUgaConsumeSupport is set to FALSE.)
505
506
507 @param This Protocol instance pointer.
508 @param Controller Handle of device to stop driver on
509 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
510 children is zero stop the entire bus driver.
511 @param ChildHandleBuffer List of Child Handles to Stop.
512
513 @retval EFI_SUCCESS This driver is removed Controller.
514 @retval EFI_NOT_STARTED Simple Text Out protocol could not be found the
515 Controller.
516 @retval other This driver was not removed from this device.
517
518 **/
519 EFI_STATUS
520 EFIAPI
521 GraphicsConsoleControllerDriverStop (
522 IN EFI_DRIVER_BINDING_PROTOCOL *This,
523 IN EFI_HANDLE Controller,
524 IN UINTN NumberOfChildren,
525 IN EFI_HANDLE *ChildHandleBuffer
526 )
527 {
528 EFI_STATUS Status;
529 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOutput;
530 GRAPHICS_CONSOLE_DEV *Private;
531
532 Status = gBS->OpenProtocol (
533 Controller,
534 &gEfiSimpleTextOutProtocolGuid,
535 (VOID **) &SimpleTextOutput,
536 This->DriverBindingHandle,
537 Controller,
538 EFI_OPEN_PROTOCOL_GET_PROTOCOL
539 );
540 if (EFI_ERROR (Status)) {
541 return EFI_NOT_STARTED;
542 }
543
544 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (SimpleTextOutput);
545
546 Status = gBS->UninstallProtocolInterface (
547 Controller,
548 &gEfiSimpleTextOutProtocolGuid,
549 &Private->SimpleTextOutput
550 );
551
552 if (!EFI_ERROR (Status)) {
553 //
554 // Close the GOP or UGA IO Protocol
555 //
556 if (Private->GraphicsOutput != NULL) {
557 gBS->CloseProtocol (
558 Controller,
559 &gEfiGraphicsOutputProtocolGuid,
560 This->DriverBindingHandle,
561 Controller
562 );
563 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
564 gBS->CloseProtocol (
565 Controller,
566 &gEfiUgaDrawProtocolGuid,
567 This->DriverBindingHandle,
568 Controller
569 );
570 }
571
572 if (Private->LineBuffer != NULL) {
573 FreePool (Private->LineBuffer);
574 }
575
576 //
577 // Free our instance data
578 //
579 FreePool (Private);
580 }
581
582 return Status;
583 }
584
585 /**
586 Check if the current specific mode supported the user defined resolution
587 for the Graphics Console device based on Graphics Output Protocol.
588
589 If yes, set the graphic devcice's current mode to this specific mode.
590
591 @param GraphicsOutput Graphics Output Protocol instance pointer.
592 @param HorizontalResolution User defined horizontal resolution
593 @param VerticalResolution User defined vertical resolution.
594 @param CurrentModeNumber Current specific mode to be check.
595
596 @retval EFI_SUCCESS The mode is supported.
597 @retval EFI_UNSUPPORTED The specific mode is out of range of graphics
598 device supported.
599 @retval other The specific mode does not support user defined
600 resolution or failed to set the current mode to the
601 specific mode on graphics device.
602
603 **/
604 EFI_STATUS
605 CheckModeSupported (
606 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput,
607 IN UINT32 HorizontalResolution,
608 IN UINT32 VerticalResolution,
609 OUT UINT32 *CurrentModeNumber
610 )
611 {
612 UINT32 ModeNumber;
613 EFI_STATUS Status;
614 UINTN SizeOfInfo;
615 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
616 UINT32 MaxMode;
617
618 Status = EFI_SUCCESS;
619 MaxMode = GraphicsOutput->Mode->MaxMode;
620
621 for (ModeNumber = 0; ModeNumber < MaxMode; ModeNumber++) {
622 Status = GraphicsOutput->QueryMode (
623 GraphicsOutput,
624 ModeNumber,
625 &SizeOfInfo,
626 &Info
627 );
628 if (!EFI_ERROR (Status)) {
629 if ((Info->HorizontalResolution == HorizontalResolution) &&
630 (Info->VerticalResolution == VerticalResolution)) {
631 if ((GraphicsOutput->Mode->Info->HorizontalResolution == HorizontalResolution) &&
632 (GraphicsOutput->Mode->Info->VerticalResolution == VerticalResolution)) {
633 //
634 // If video device has been set to this mode, we do not need to SetMode again
635 //
636 break;
637 } else {
638 Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);
639 if (!EFI_ERROR (Status)) {
640 FreePool (Info);
641 break;
642 }
643 }
644 }
645 FreePool (Info);
646 }
647 }
648
649 if (ModeNumber == GraphicsOutput->Mode->MaxMode) {
650 Status = EFI_UNSUPPORTED;
651 }
652
653 *CurrentModeNumber = ModeNumber;
654 return Status;
655 }
656
657
658 /**
659 Locate HII Database protocol and HII Font protocol.
660
661 @retval EFI_SUCCESS HII Database protocol and HII Font protocol
662 are located successfully.
663 @return other Failed to locate HII Database protocol or
664 HII Font protocol.
665
666 **/
667 EFI_STATUS
668 EfiLocateHiiProtocol (
669 VOID
670 )
671 {
672 EFI_HANDLE Handle;
673 UINTN Size;
674 EFI_STATUS Status;
675
676 //
677 // There should only be one - so buffer size is this
678 //
679 Size = sizeof (EFI_HANDLE);
680
681 Status = gBS->LocateHandle (
682 ByProtocol,
683 &gEfiHiiDatabaseProtocolGuid,
684 NULL,
685 &Size,
686 (VOID **) &Handle
687 );
688
689 if (EFI_ERROR (Status)) {
690 return Status;
691 }
692
693 Status = gBS->HandleProtocol (
694 Handle,
695 &gEfiHiiDatabaseProtocolGuid,
696 (VOID **) &mHiiDatabase
697 );
698
699 if (EFI_ERROR (Status)) {
700 return Status;
701 }
702
703 Status = gBS->HandleProtocol (
704 Handle,
705 &gEfiHiiFontProtocolGuid,
706 (VOID **) &mHiiFont
707 );
708 return Status;
709 }
710
711 //
712 // Body of the STO functions
713 //
714
715 /**
716 Reset the text output device hardware and optionally run diagnostics.
717
718 Implements SIMPLE_TEXT_OUTPUT.Reset().
719 If ExtendeVerification is TRUE, then perform dependent Graphics Console
720 device reset, and set display mode to mode 0.
721 If ExtendedVerification is FALSE, only set display mode to mode 0.
722
723 @param This Protocol instance pointer.
724 @param ExtendedVerification Indicates that the driver may perform a more
725 exhaustive verification operation of the device
726 during reset.
727
728 @retval EFI_SUCCESS The text output device was reset.
729 @retval EFI_DEVICE_ERROR The text output device is not functioning correctly and
730 could not be reset.
731
732 **/
733 EFI_STATUS
734 EFIAPI
735 GraphicsConsoleConOutReset (
736 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
737 IN BOOLEAN ExtendedVerification
738 )
739 {
740 This->SetAttribute (This, EFI_TEXT_ATTR (This->Mode->Attribute & 0x0F, EFI_BACKGROUND_BLACK));
741 return This->SetMode (This, 0);
742 }
743
744
745 /**
746 Write a Unicode string to the output device.
747
748 Implements SIMPLE_TEXT_OUTPUT.OutputString().
749 The Unicode string will be converted to Glyphs and will be
750 sent to the Graphics Console.
751
752 @param This Protocol instance pointer.
753 @param WString The NULL-terminated Unicode string to be displayed
754 on the output device(s). All output devices must
755 also support the Unicode drawing defined in this file.
756
757 @retval EFI_SUCCESS The string was output to the device.
758 @retval EFI_DEVICE_ERROR The device reported an error while attempting to output
759 the text.
760 @retval EFI_UNSUPPORTED The output device's mode is not currently in a
761 defined text mode.
762 @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the
763 characters in the Unicode string could not be
764 rendered and were skipped.
765
766 **/
767 EFI_STATUS
768 EFIAPI
769 GraphicsConsoleConOutOutputString (
770 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
771 IN CHAR16 *WString
772 )
773 {
774 GRAPHICS_CONSOLE_DEV *Private;
775 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
776 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
777 INTN Mode;
778 UINTN MaxColumn;
779 UINTN MaxRow;
780 UINTN Width;
781 UINTN Height;
782 UINTN Delta;
783 EFI_STATUS Status;
784 BOOLEAN Warning;
785 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Foreground;
786 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;
787 UINTN DeltaX;
788 UINTN DeltaY;
789 UINTN Count;
790 UINTN Index;
791 INT32 OriginAttribute;
792 EFI_TPL OldTpl;
793
794 Status = EFI_SUCCESS;
795
796 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
797 //
798 // Current mode
799 //
800 Mode = This->Mode->Mode;
801 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
802 GraphicsOutput = Private->GraphicsOutput;
803 UgaDraw = Private->UgaDraw;
804
805 MaxColumn = Private->ModeData[Mode].Columns;
806 MaxRow = Private->ModeData[Mode].Rows;
807 DeltaX = (UINTN) Private->ModeData[Mode].DeltaX;
808 DeltaY = (UINTN) Private->ModeData[Mode].DeltaY;
809 Width = MaxColumn * EFI_GLYPH_WIDTH;
810 Height = (MaxRow - 1) * EFI_GLYPH_HEIGHT;
811 Delta = Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
812
813 //
814 // The Attributes won't change when during the time OutputString is called
815 //
816 GetTextColors (This, &Foreground, &Background);
817
818 EraseCursor (This);
819
820 Warning = FALSE;
821
822 //
823 // Backup attribute
824 //
825 OriginAttribute = This->Mode->Attribute;
826
827 while (*WString != L'\0') {
828
829 if (*WString == CHAR_BACKSPACE) {
830 //
831 // If the cursor is at the left edge of the display, then move the cursor
832 // one row up.
833 //
834 if (This->Mode->CursorColumn == 0 && This->Mode->CursorRow > 0) {
835 This->Mode->CursorRow--;
836 This->Mode->CursorColumn = (INT32) (MaxColumn - 1);
837 This->OutputString (This, SpaceStr);
838 EraseCursor (This);
839 This->Mode->CursorRow--;
840 This->Mode->CursorColumn = (INT32) (MaxColumn - 1);
841 } else if (This->Mode->CursorColumn > 0) {
842 //
843 // If the cursor is not at the left edge of the display, then move the cursor
844 // left one column.
845 //
846 This->Mode->CursorColumn--;
847 This->OutputString (This, SpaceStr);
848 EraseCursor (This);
849 This->Mode->CursorColumn--;
850 }
851
852 WString++;
853
854 } else if (*WString == CHAR_LINEFEED) {
855 //
856 // If the cursor is at the bottom of the display, then scroll the display one
857 // row, and do not update the cursor position. Otherwise, move the cursor
858 // down one row.
859 //
860 if (This->Mode->CursorRow == (INT32) (MaxRow - 1)) {
861 if (GraphicsOutput != NULL) {
862 //
863 // Scroll Screen Up One Row
864 //
865 GraphicsOutput->Blt (
866 GraphicsOutput,
867 NULL,
868 EfiBltVideoToVideo,
869 DeltaX,
870 DeltaY + EFI_GLYPH_HEIGHT,
871 DeltaX,
872 DeltaY,
873 Width,
874 Height,
875 Delta
876 );
877
878 //
879 // Print Blank Line at last line
880 //
881 GraphicsOutput->Blt (
882 GraphicsOutput,
883 &Background,
884 EfiBltVideoFill,
885 0,
886 0,
887 DeltaX,
888 DeltaY + Height,
889 Width,
890 EFI_GLYPH_HEIGHT,
891 Delta
892 );
893 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
894 //
895 // Scroll Screen Up One Row
896 //
897 UgaDraw->Blt (
898 UgaDraw,
899 NULL,
900 EfiUgaVideoToVideo,
901 DeltaX,
902 DeltaY + EFI_GLYPH_HEIGHT,
903 DeltaX,
904 DeltaY,
905 Width,
906 Height,
907 Delta
908 );
909
910 //
911 // Print Blank Line at last line
912 //
913 UgaDraw->Blt (
914 UgaDraw,
915 (EFI_UGA_PIXEL *) (UINTN) &Background,
916 EfiUgaVideoFill,
917 0,
918 0,
919 DeltaX,
920 DeltaY + Height,
921 Width,
922 EFI_GLYPH_HEIGHT,
923 Delta
924 );
925 }
926 } else {
927 This->Mode->CursorRow++;
928 }
929
930 WString++;
931
932 } else if (*WString == CHAR_CARRIAGE_RETURN) {
933 //
934 // Move the cursor to the beginning of the current row.
935 //
936 This->Mode->CursorColumn = 0;
937 WString++;
938
939 } else if (*WString == WIDE_CHAR) {
940
941 This->Mode->Attribute |= EFI_WIDE_ATTRIBUTE;
942 WString++;
943
944 } else if (*WString == NARROW_CHAR) {
945
946 This->Mode->Attribute &= (~ (UINT32) EFI_WIDE_ATTRIBUTE);
947 WString++;
948
949 } else {
950 //
951 // Print the character at the current cursor position and move the cursor
952 // right one column. If this moves the cursor past the right edge of the
953 // display, then the line should wrap to the beginning of the next line. This
954 // is equivalent to inserting a CR and an LF. Note that if the cursor is at the
955 // bottom of the display, and the line wraps, then the display will be scrolled
956 // one line.
957 // If wide char is going to be displayed, need to display one character at a time
958 // Or, need to know the display length of a certain string.
959 //
960 // Index is used to determine how many character width units (wide = 2, narrow = 1)
961 // Count is used to determine how many characters are used regardless of their attributes
962 //
963 for (Count = 0, Index = 0; (This->Mode->CursorColumn + Index) < MaxColumn; Count++, Index++) {
964 if (WString[Count] == CHAR_NULL ||
965 WString[Count] == CHAR_BACKSPACE ||
966 WString[Count] == CHAR_LINEFEED ||
967 WString[Count] == CHAR_CARRIAGE_RETURN ||
968 WString[Count] == WIDE_CHAR ||
969 WString[Count] == NARROW_CHAR) {
970 break;
971 }
972 //
973 // Is the wide attribute on?
974 //
975 if ((This->Mode->Attribute & EFI_WIDE_ATTRIBUTE) != 0) {
976 //
977 // If wide, add one more width unit than normal since we are going to increment at the end of the for loop
978 //
979 Index++;
980 //
981 // This is the end-case where if we are at column 79 and about to print a wide character
982 // We should prevent this from happening because we will wrap inappropriately. We should
983 // not print this character until the next line.
984 //
985 if ((This->Mode->CursorColumn + Index + 1) > MaxColumn) {
986 Index++;
987 break;
988 }
989 }
990 }
991
992 Status = DrawUnicodeWeightAtCursorN (This, WString, Count);
993 if (EFI_ERROR (Status)) {
994 Warning = TRUE;
995 }
996 //
997 // At the end of line, output carriage return and line feed
998 //
999 WString += Count;
1000 This->Mode->CursorColumn += (INT32) Index;
1001 if (This->Mode->CursorColumn > (INT32) MaxColumn) {
1002 This->Mode->CursorColumn -= 2;
1003 This->OutputString (This, SpaceStr);
1004 }
1005
1006 if (This->Mode->CursorColumn >= (INT32) MaxColumn) {
1007 EraseCursor (This);
1008 This->OutputString (This, mCrLfString);
1009 EraseCursor (This);
1010 }
1011 }
1012 }
1013
1014 This->Mode->Attribute = OriginAttribute;
1015
1016 EraseCursor (This);
1017
1018 if (Warning) {
1019 Status = EFI_WARN_UNKNOWN_GLYPH;
1020 }
1021
1022 gBS->RestoreTPL (OldTpl);
1023 return Status;
1024
1025 }
1026
1027 /**
1028 Verifies that all characters in a Unicode string can be output to the
1029 target device.
1030
1031 Implements SIMPLE_TEXT_OUTPUT.TestString().
1032 If one of the characters in the *Wstring is neither valid valid Unicode
1033 drawing characters, not ASCII code, then this function will return
1034 EFI_UNSUPPORTED
1035
1036 @param This Protocol instance pointer.
1037 @param WString The NULL-terminated Unicode string to be examined for the output
1038 device(s).
1039
1040 @retval EFI_SUCCESS The device(s) are capable of rendering the output string.
1041 @retval EFI_UNSUPPORTED Some of the characters in the Unicode string cannot be
1042 rendered by one or more of the output devices mapped
1043 by the EFI handle.
1044
1045 **/
1046 EFI_STATUS
1047 EFIAPI
1048 GraphicsConsoleConOutTestString (
1049 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1050 IN CHAR16 *WString
1051 )
1052 {
1053 EFI_STATUS Status;
1054 UINT16 Count;
1055
1056 EFI_IMAGE_OUTPUT *Blt;
1057
1058 Blt = NULL;
1059 Count = 0;
1060
1061 while (WString[Count] != 0) {
1062 Status = mHiiFont->GetGlyph (
1063 mHiiFont,
1064 WString[Count],
1065 NULL,
1066 &Blt,
1067 NULL
1068 );
1069 if (Blt != NULL) {
1070 FreePool (Blt);
1071 Blt = NULL;
1072 }
1073 Count++;
1074
1075 if (EFI_ERROR (Status)) {
1076 return EFI_UNSUPPORTED;
1077 }
1078 }
1079
1080 return EFI_SUCCESS;
1081 }
1082
1083
1084 /**
1085 Returns information for an available text mode that the output device(s)
1086 supports
1087
1088 Implements SIMPLE_TEXT_OUTPUT.QueryMode().
1089 It returnes information for an available text mode that the Graphics Console supports.
1090 In this driver,we only support text mode 80x25, which is defined as mode 0.
1091
1092 @param This Protocol instance pointer.
1093 @param ModeNumber The mode number to return information on.
1094 @param Columns The returned columns of the requested mode.
1095 @param Rows The returned rows of the requested mode.
1096
1097 @retval EFI_SUCCESS The requested mode information is returned.
1098 @retval EFI_UNSUPPORTED The mode number is not valid.
1099
1100 **/
1101 EFI_STATUS
1102 EFIAPI
1103 GraphicsConsoleConOutQueryMode (
1104 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1105 IN UINTN ModeNumber,
1106 OUT UINTN *Columns,
1107 OUT UINTN *Rows
1108 )
1109 {
1110 GRAPHICS_CONSOLE_DEV *Private;
1111 EFI_STATUS Status;
1112 EFI_TPL OldTpl;
1113
1114 if (ModeNumber >= (UINTN) This->Mode->MaxMode) {
1115 return EFI_UNSUPPORTED;
1116 }
1117
1118 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1119 Status = EFI_SUCCESS;
1120
1121 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1122
1123 *Columns = Private->ModeData[ModeNumber].Columns;
1124 *Rows = Private->ModeData[ModeNumber].Rows;
1125
1126 if (*Columns <= 0 && *Rows <= 0) {
1127 Status = EFI_UNSUPPORTED;
1128 goto Done;
1129
1130 }
1131
1132 Done:
1133 gBS->RestoreTPL (OldTpl);
1134 return Status;
1135 }
1136
1137
1138 /**
1139 Sets the output device(s) to a specified mode.
1140
1141 Implements SIMPLE_TEXT_OUTPUT.SetMode().
1142 Set the Graphics Console to a specified mode. In this driver, we only support mode 0.
1143
1144 @param This Protocol instance pointer.
1145 @param ModeNumber The text mode to set.
1146
1147 @retval EFI_SUCCESS The requested text mode is set.
1148 @retval EFI_DEVICE_ERROR The requested text mode cannot be set because of
1149 Graphics Console device error.
1150 @retval EFI_UNSUPPORTED The text mode number is not valid.
1151
1152 **/
1153 EFI_STATUS
1154 EFIAPI
1155 GraphicsConsoleConOutSetMode (
1156 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1157 IN UINTN ModeNumber
1158 )
1159 {
1160 EFI_STATUS Status;
1161 GRAPHICS_CONSOLE_DEV *Private;
1162 GRAPHICS_CONSOLE_MODE_DATA *ModeData;
1163 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *NewLineBuffer;
1164 UINT32 HorizontalResolution;
1165 UINT32 VerticalResolution;
1166 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
1167 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
1168 UINT32 ColorDepth;
1169 UINT32 RefreshRate;
1170 EFI_TPL OldTpl;
1171
1172 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1173
1174 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1175 GraphicsOutput = Private->GraphicsOutput;
1176 UgaDraw = Private->UgaDraw;
1177 ModeData = &(Private->ModeData[ModeNumber]);
1178
1179 //
1180 // Make sure the requested mode number is supported
1181 //
1182 if (ModeNumber >= (UINTN) This->Mode->MaxMode) {
1183 Status = EFI_UNSUPPORTED;
1184 goto Done;
1185 }
1186
1187 if (ModeData->Columns <= 0 && ModeData->Rows <= 0) {
1188 Status = EFI_UNSUPPORTED;
1189 goto Done;
1190 }
1191 //
1192 // Attempt to allocate a line buffer for the requested mode number
1193 //
1194 NewLineBuffer = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * ModeData->Columns * EFI_GLYPH_WIDTH * EFI_GLYPH_HEIGHT);
1195
1196 if (NewLineBuffer == NULL) {
1197 //
1198 // The new line buffer could not be allocated, so return an error.
1199 // No changes to the state of the current console have been made, so the current console is still valid
1200 //
1201 Status = EFI_OUT_OF_RESOURCES;
1202 goto Done;
1203 }
1204 //
1205 // If the mode has been set at least one other time, then LineBuffer will not be NULL
1206 //
1207 if (Private->LineBuffer != NULL) {
1208 //
1209 // Clear the current text window on the current graphics console
1210 //
1211 This->ClearScreen (This);
1212
1213 //
1214 // If the new mode is the same as the old mode, then just return EFI_SUCCESS
1215 //
1216 if ((INT32) ModeNumber == This->Mode->Mode) {
1217 FreePool (NewLineBuffer);
1218 Status = EFI_SUCCESS;
1219 goto Done;
1220 }
1221 //
1222 // Otherwise, the size of the text console and/or the GOP/UGA mode will be changed,
1223 // so erase the cursor, and free the LineBuffer for the current mode
1224 //
1225 EraseCursor (This);
1226
1227 FreePool (Private->LineBuffer);
1228 }
1229 //
1230 // Assign the current line buffer to the newly allocated line buffer
1231 //
1232 Private->LineBuffer = NewLineBuffer;
1233
1234 if (GraphicsOutput != NULL) {
1235 if (ModeData->GopModeNumber != GraphicsOutput->Mode->Mode) {
1236 //
1237 // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new graphics mode
1238 //
1239 Status = GraphicsOutput->SetMode (GraphicsOutput, ModeData->GopModeNumber);
1240 if (EFI_ERROR (Status)) {
1241 //
1242 // The mode set operation failed
1243 //
1244 goto Done;
1245 }
1246 } else {
1247 //
1248 // The current graphics mode is correct, so simply clear the entire display
1249 //
1250 Status = GraphicsOutput->Blt (
1251 GraphicsOutput,
1252 &mGraphicsEfiColors[0],
1253 EfiBltVideoFill,
1254 0,
1255 0,
1256 0,
1257 0,
1258 ModeData->GopWidth,
1259 ModeData->GopHeight,
1260 0
1261 );
1262 }
1263 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
1264 //
1265 // Get the current UGA Draw mode information
1266 //
1267 Status = UgaDraw->GetMode (
1268 UgaDraw,
1269 &HorizontalResolution,
1270 &VerticalResolution,
1271 &ColorDepth,
1272 &RefreshRate
1273 );
1274 if (EFI_ERROR (Status) || HorizontalResolution != ModeData->GopWidth || VerticalResolution != ModeData->GopHeight) {
1275 //
1276 // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new graphics mode
1277 //
1278 Status = UgaDraw->SetMode (
1279 UgaDraw,
1280 ModeData->GopWidth,
1281 ModeData->GopHeight,
1282 32,
1283 60
1284 );
1285 if (EFI_ERROR (Status)) {
1286 //
1287 // The mode set operation failed
1288 //
1289 goto Done;
1290 }
1291 } else {
1292 //
1293 // The current graphics mode is correct, so simply clear the entire display
1294 //
1295 Status = UgaDraw->Blt (
1296 UgaDraw,
1297 (EFI_UGA_PIXEL *) (UINTN) &mGraphicsEfiColors[0],
1298 EfiUgaVideoFill,
1299 0,
1300 0,
1301 0,
1302 0,
1303 ModeData->GopWidth,
1304 ModeData->GopHeight,
1305 0
1306 );
1307 }
1308 }
1309
1310 //
1311 // The new mode is valid, so commit the mode change
1312 //
1313 This->Mode->Mode = (INT32) ModeNumber;
1314
1315 //
1316 // Move the text cursor to the upper left hand corner of the display and enable it
1317 //
1318 This->SetCursorPosition (This, 0, 0);
1319
1320 Status = EFI_SUCCESS;
1321
1322 Done:
1323 gBS->RestoreTPL (OldTpl);
1324 return Status;
1325 }
1326
1327
1328 /**
1329 Sets the background and foreground colors for the OutputString () and
1330 ClearScreen () functions.
1331
1332 Implements SIMPLE_TEXT_OUTPUT.SetAttribute().
1333
1334 @param This Protocol instance pointer.
1335 @param Attribute The attribute to set. Bits 0..3 are the foreground
1336 color, and bits 4..6 are the background color.
1337 All other bits are undefined and must be zero.
1338
1339 @retval EFI_SUCCESS The requested attribute is set.
1340 @retval EFI_DEVICE_ERROR The requested attribute cannot be set due to Graphics Console port error.
1341 @retval EFI_UNSUPPORTED The attribute requested is not defined.
1342
1343 **/
1344 EFI_STATUS
1345 EFIAPI
1346 GraphicsConsoleConOutSetAttribute (
1347 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1348 IN UINTN Attribute
1349 )
1350 {
1351 EFI_TPL OldTpl;
1352
1353 if ((Attribute | 0xFF) != 0xFF) {
1354 return EFI_UNSUPPORTED;
1355 }
1356
1357 if ((INT32) Attribute == This->Mode->Attribute) {
1358 return EFI_SUCCESS;
1359 }
1360
1361 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1362
1363 EraseCursor (This);
1364
1365 This->Mode->Attribute = (INT32) Attribute;
1366
1367 EraseCursor (This);
1368
1369 gBS->RestoreTPL (OldTpl);
1370
1371 return EFI_SUCCESS;
1372 }
1373
1374
1375 /**
1376 Clears the output device(s) display to the currently selected background
1377 color.
1378
1379 Implements SIMPLE_TEXT_OUTPUT.ClearScreen().
1380
1381 @param This Protocol instance pointer.
1382
1383 @retval EFI_SUCCESS The operation completed successfully.
1384 @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.
1385 @retval EFI_UNSUPPORTED The output device is not in a valid text mode.
1386
1387 **/
1388 EFI_STATUS
1389 EFIAPI
1390 GraphicsConsoleConOutClearScreen (
1391 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
1392 )
1393 {
1394 EFI_STATUS Status;
1395 GRAPHICS_CONSOLE_DEV *Private;
1396 GRAPHICS_CONSOLE_MODE_DATA *ModeData;
1397 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
1398 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
1399 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Foreground;
1400 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;
1401 EFI_TPL OldTpl;
1402
1403 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1404
1405 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1406 GraphicsOutput = Private->GraphicsOutput;
1407 UgaDraw = Private->UgaDraw;
1408 ModeData = &(Private->ModeData[This->Mode->Mode]);
1409
1410 GetTextColors (This, &Foreground, &Background);
1411 if (GraphicsOutput != NULL) {
1412 Status = GraphicsOutput->Blt (
1413 GraphicsOutput,
1414 &Background,
1415 EfiBltVideoFill,
1416 0,
1417 0,
1418 0,
1419 0,
1420 ModeData->GopWidth,
1421 ModeData->GopHeight,
1422 0
1423 );
1424 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
1425 Status = UgaDraw->Blt (
1426 UgaDraw,
1427 (EFI_UGA_PIXEL *) (UINTN) &Background,
1428 EfiUgaVideoFill,
1429 0,
1430 0,
1431 0,
1432 0,
1433 ModeData->GopWidth,
1434 ModeData->GopHeight,
1435 0
1436 );
1437 } else {
1438 Status = EFI_UNSUPPORTED;
1439 }
1440
1441 This->Mode->CursorColumn = 0;
1442 This->Mode->CursorRow = 0;
1443
1444 EraseCursor (This);
1445
1446 gBS->RestoreTPL (OldTpl);
1447
1448 return Status;
1449 }
1450
1451
1452 /**
1453 Sets the current coordinates of the cursor position.
1454
1455 Implements SIMPLE_TEXT_OUTPUT.SetCursorPosition().
1456
1457 @param This Protocol instance pointer.
1458 @param Column The position to set the cursor to. Must be greater than or
1459 equal to zero and less than the number of columns and rows
1460 by QueryMode ().
1461 @param Row The position to set the cursor to. Must be greater than or
1462 equal to zero and less than the number of columns and rows
1463 by QueryMode ().
1464
1465 @retval EFI_SUCCESS The operation completed successfully.
1466 @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.
1467 @retval EFI_UNSUPPORTED The output device is not in a valid text mode, or the
1468 cursor position is invalid for the current mode.
1469
1470 **/
1471 EFI_STATUS
1472 EFIAPI
1473 GraphicsConsoleConOutSetCursorPosition (
1474 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1475 IN UINTN Column,
1476 IN UINTN Row
1477 )
1478 {
1479 GRAPHICS_CONSOLE_DEV *Private;
1480 GRAPHICS_CONSOLE_MODE_DATA *ModeData;
1481 EFI_STATUS Status;
1482 EFI_TPL OldTpl;
1483
1484 Status = EFI_SUCCESS;
1485
1486 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1487
1488 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1489 ModeData = &(Private->ModeData[This->Mode->Mode]);
1490
1491 if ((Column >= ModeData->Columns) || (Row >= ModeData->Rows)) {
1492 Status = EFI_UNSUPPORTED;
1493 goto Done;
1494 }
1495
1496 if ((This->Mode->CursorColumn == (INT32) Column) && (This->Mode->CursorRow == (INT32) Row)) {
1497 Status = EFI_SUCCESS;
1498 goto Done;
1499 }
1500
1501 EraseCursor (This);
1502
1503 This->Mode->CursorColumn = (INT32) Column;
1504 This->Mode->CursorRow = (INT32) Row;
1505
1506 EraseCursor (This);
1507
1508 Done:
1509 gBS->RestoreTPL (OldTpl);
1510
1511 return Status;
1512 }
1513
1514
1515 /**
1516 Makes the cursor visible or invisible.
1517
1518 Implements SIMPLE_TEXT_OUTPUT.EnableCursor().
1519
1520 @param This Protocol instance pointer.
1521 @param Visible If TRUE, the cursor is set to be visible, If FALSE,
1522 the cursor is set to be invisible.
1523
1524 @retval EFI_SUCCESS The operation completed successfully.
1525
1526 **/
1527 EFI_STATUS
1528 EFIAPI
1529 GraphicsConsoleConOutEnableCursor (
1530 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1531 IN BOOLEAN Visible
1532 )
1533 {
1534 EFI_TPL OldTpl;
1535
1536 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1537
1538 EraseCursor (This);
1539
1540 This->Mode->CursorVisible = Visible;
1541
1542 EraseCursor (This);
1543
1544 gBS->RestoreTPL (OldTpl);
1545 return EFI_SUCCESS;
1546 }
1547
1548 /**
1549 Gets Graphics Console devcie's foreground color and background color.
1550
1551 @param This Protocol instance pointer.
1552 @param Foreground Returned text foreground color.
1553 @param Background Returned text background color.
1554
1555 @retval EFI_SUCCESS It returned always.
1556
1557 **/
1558 EFI_STATUS
1559 GetTextColors (
1560 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1561 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground,
1562 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background
1563 )
1564 {
1565 INTN Attribute;
1566
1567 Attribute = This->Mode->Attribute & 0x7F;
1568
1569 *Foreground = mGraphicsEfiColors[Attribute & 0x0f];
1570 *Background = mGraphicsEfiColors[Attribute >> 4];
1571
1572 return EFI_SUCCESS;
1573 }
1574
1575 /**
1576 Draw Unicode string on the Graphics Console device's screen.
1577
1578 @param This Protocol instance pointer.
1579 @param UnicodeWeight One Unicode string to be displayed.
1580 @param Count The count of Unicode string.
1581
1582 @retval EFI_OUT_OF_RESOURCES If no memory resource to use.
1583 @retval EFI_UNSUPPORTED If no Graphics Output protocol and UGA Draw
1584 protocol exist.
1585 @retval EFI_SUCCESS Drawing Unicode string implemented successfully.
1586
1587 **/
1588 EFI_STATUS
1589 DrawUnicodeWeightAtCursorN (
1590 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
1591 IN CHAR16 *UnicodeWeight,
1592 IN UINTN Count
1593 )
1594 {
1595 EFI_STATUS Status;
1596 GRAPHICS_CONSOLE_DEV *Private;
1597 EFI_IMAGE_OUTPUT *Blt;
1598 EFI_STRING String;
1599 EFI_FONT_DISPLAY_INFO *FontInfo;
1600 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
1601 EFI_HII_ROW_INFO *RowInfoArray;
1602 UINTN RowInfoArraySize;
1603
1604 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1605 Blt = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));
1606 if (Blt == NULL) {
1607 return EFI_OUT_OF_RESOURCES;
1608 }
1609
1610 Blt->Width = (UINT16) (Private->ModeData[This->Mode->Mode].GopWidth);
1611 Blt->Height = (UINT16) (Private->ModeData[This->Mode->Mode].GopHeight);
1612
1613 String = AllocateCopyPool ((Count + 1) * sizeof (CHAR16), UnicodeWeight);
1614 if (String == NULL) {
1615 FreePool (Blt);
1616 return EFI_OUT_OF_RESOURCES;
1617 }
1618 //
1619 // Set the end character
1620 //
1621 *(String + Count) = L'\0';
1622
1623 FontInfo = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (sizeof (EFI_FONT_DISPLAY_INFO));
1624 if (FontInfo == NULL) {
1625 FreePool (Blt);
1626 FreePool (String);
1627 return EFI_OUT_OF_RESOURCES;
1628 }
1629 //
1630 // Get current foreground and background colors.
1631 //
1632 GetTextColors (This, &FontInfo->ForegroundColor, &FontInfo->BackgroundColor);
1633
1634 if (Private->GraphicsOutput != NULL) {
1635 //
1636 // If Graphics Output protocol exists, using HII Font protocol to draw.
1637 //
1638 Blt->Image.Screen = Private->GraphicsOutput;
1639
1640 Status = mHiiFont->StringToImage (
1641 mHiiFont,
1642 EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_DIRECT_TO_SCREEN | EFI_HII_IGNORE_LINE_BREAK,
1643 String,
1644 FontInfo,
1645 &Blt,
1646 This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,
1647 This->Mode->CursorRow * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,
1648 NULL,
1649 NULL,
1650 NULL
1651 );
1652
1653 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
1654 //
1655 // If Graphics Output protocol cannot be found and PcdUgaConsumeSupport enabled,
1656 // using UGA Draw protocol to draw.
1657 //
1658 ASSERT (Private->UgaDraw!= NULL);
1659
1660 UgaDraw = Private->UgaDraw;
1661
1662 Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
1663 if (Blt->Image.Bitmap == NULL) {
1664 FreePool (Blt);
1665 FreePool (String);
1666 return EFI_OUT_OF_RESOURCES;
1667 }
1668
1669 RowInfoArray = NULL;
1670 //
1671 // StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,
1672 // we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.
1673 //
1674 Status = mHiiFont->StringToImage (
1675 mHiiFont,
1676 EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_IGNORE_LINE_BREAK,
1677 String,
1678 FontInfo,
1679 &Blt,
1680 This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,
1681 This->Mode->CursorRow * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,
1682 &RowInfoArray,
1683 &RowInfoArraySize,
1684 NULL
1685 );
1686
1687 if (!EFI_ERROR (Status)) {
1688 //
1689 // Line breaks are handled by caller of DrawUnicodeWeightAtCursorN, so the updated parameter RowInfoArraySize by StringToImage will
1690 // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.
1691 //
1692 ASSERT (RowInfoArraySize <= 1);
1693
1694 Status = UgaDraw->Blt (
1695 UgaDraw,
1696 (EFI_UGA_PIXEL *) Blt->Image.Bitmap,
1697 EfiUgaBltBufferToVideo,
1698 This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,
1699 (This->Mode->CursorRow) * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,
1700 This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,
1701 (This->Mode->CursorRow) * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,
1702 RowInfoArray[0].LineWidth,
1703 RowInfoArray[0].LineHeight,
1704 Blt->Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
1705 );
1706 }
1707
1708 FreePool (RowInfoArray);
1709 FreePool (Blt->Image.Bitmap);
1710 } else {
1711 Status = EFI_UNSUPPORTED;
1712 }
1713
1714 if (Blt != NULL) {
1715 FreePool (Blt);
1716 }
1717 if (String != NULL) {
1718 FreePool (String);
1719 }
1720 if (FontInfo != NULL) {
1721 FreePool (FontInfo);
1722 }
1723 return Status;
1724 }
1725
1726 /**
1727 Erase the cursor on the screen.
1728
1729 @param This Protocol instance pointer.
1730
1731 @retval EFI_SUCCESS The cursor is erased successfully.
1732
1733 **/
1734 EFI_STATUS
1735 EraseCursor (
1736 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
1737 )
1738 {
1739 GRAPHICS_CONSOLE_DEV *Private;
1740 EFI_SIMPLE_TEXT_OUTPUT_MODE *CurrentMode;
1741 INTN GlyphX;
1742 INTN GlyphY;
1743 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
1744 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
1745 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Foreground;
1746 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Background;
1747 EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION BltChar[EFI_GLYPH_HEIGHT][EFI_GLYPH_WIDTH];
1748 UINTN PosX;
1749 UINTN PosY;
1750
1751 CurrentMode = This->Mode;
1752
1753 if (!CurrentMode->CursorVisible) {
1754 return EFI_SUCCESS;
1755 }
1756
1757 Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);
1758 GraphicsOutput = Private->GraphicsOutput;
1759 UgaDraw = Private->UgaDraw;
1760
1761 //
1762 // In this driver, only narrow character was supported.
1763 //
1764 //
1765 // Blt a character to the screen
1766 //
1767 GlyphX = (CurrentMode->CursorColumn * EFI_GLYPH_WIDTH) + Private->ModeData[CurrentMode->Mode].DeltaX;
1768 GlyphY = (CurrentMode->CursorRow * EFI_GLYPH_HEIGHT) + Private->ModeData[CurrentMode->Mode].DeltaY;
1769 if (GraphicsOutput != NULL) {
1770 GraphicsOutput->Blt (
1771 GraphicsOutput,
1772 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) BltChar,
1773 EfiBltVideoToBltBuffer,
1774 GlyphX,
1775 GlyphY,
1776 0,
1777 0,
1778 EFI_GLYPH_WIDTH,
1779 EFI_GLYPH_HEIGHT,
1780 EFI_GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
1781 );
1782 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
1783 UgaDraw->Blt (
1784 UgaDraw,
1785 (EFI_UGA_PIXEL *) (UINTN) BltChar,
1786 EfiUgaVideoToBltBuffer,
1787 GlyphX,
1788 GlyphY,
1789 0,
1790 0,
1791 EFI_GLYPH_WIDTH,
1792 EFI_GLYPH_HEIGHT,
1793 EFI_GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)
1794 );
1795 }
1796
1797 GetTextColors (This, &Foreground.Pixel, &Background.Pixel);
1798
1799 //
1800 // Convert Monochrome bitmap of the Glyph to BltBuffer structure
1801 //
1802 for (PosY = 0; PosY < EFI_GLYPH_HEIGHT; PosY++) {
1803 for (PosX = 0; PosX < EFI_GLYPH_WIDTH; PosX++) {
1804 if ((mCursorGlyph.GlyphCol1[PosY] & (BIT0 << PosX)) != 0) {
1805 BltChar[PosY][EFI_GLYPH_WIDTH - PosX - 1].Raw ^= Foreground.Raw;
1806 }
1807 }
1808 }
1809
1810 if (GraphicsOutput != NULL) {
1811 GraphicsOutput->Blt (
1812 GraphicsOutput,
1813 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) BltChar,
1814 EfiBltBufferToVideo,
1815 0,
1816 0,
1817 GlyphX,
1818 GlyphY,
1819 EFI_GLYPH_WIDTH,
1820 EFI_GLYPH_HEIGHT,
1821 EFI_GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
1822 );
1823 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
1824 UgaDraw->Blt (
1825 UgaDraw,
1826 (EFI_UGA_PIXEL *) (UINTN) BltChar,
1827 EfiUgaBltBufferToVideo,
1828 0,
1829 0,
1830 GlyphX,
1831 GlyphY,
1832 EFI_GLYPH_WIDTH,
1833 EFI_GLYPH_HEIGHT,
1834 EFI_GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)
1835 );
1836 }
1837
1838 return EFI_SUCCESS;
1839 }
1840
1841 /**
1842 HII Database Protocol notification event handler.
1843
1844 Register font package when HII Database Protocol has been installed.
1845
1846 @param[in] Event Event whose notification function is being invoked.
1847 @param[in] Context Pointer to the notification function's context.
1848 **/
1849 VOID
1850 EFIAPI
1851 RegisterFontPackage (
1852 IN EFI_EVENT Event,
1853 IN VOID *Context
1854 )
1855 {
1856 EFI_STATUS Status;
1857 EFI_HII_SIMPLE_FONT_PACKAGE_HDR *SimplifiedFont;
1858 UINT32 PackageLength;
1859 UINT8 *Package;
1860 UINT8 *Location;
1861 EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
1862
1863 //
1864 // Locate HII Database Protocol
1865 //
1866 Status = gBS->LocateProtocol (
1867 &gEfiHiiDatabaseProtocolGuid,
1868 NULL,
1869 (VOID **) &HiiDatabase
1870 );
1871 ASSERT_EFI_ERROR (Status);
1872
1873 //
1874 // Add 4 bytes to the header for entire length for HiiAddPackages use only.
1875 //
1876 // +--------------------------------+ <-- Package
1877 // | |
1878 // | PackageLength(4 bytes) |
1879 // | |
1880 // |--------------------------------| <-- SimplifiedFont
1881 // | |
1882 // |EFI_HII_SIMPLE_FONT_PACKAGE_HDR |
1883 // | |
1884 // |--------------------------------| <-- Location
1885 // | |
1886 // | gUsStdNarrowGlyphData |
1887 // | |
1888 // +--------------------------------+
1889
1890 PackageLength = sizeof (EFI_HII_SIMPLE_FONT_PACKAGE_HDR) + mNarrowFontSize + 4;
1891 Package = AllocateZeroPool (PackageLength);
1892 ASSERT (Package != NULL);
1893
1894 WriteUnaligned32((UINT32 *) Package,PackageLength);
1895 SimplifiedFont = (EFI_HII_SIMPLE_FONT_PACKAGE_HDR *) (Package + 4);
1896 SimplifiedFont->Header.Length = (UINT32) (PackageLength - 4);
1897 SimplifiedFont->Header.Type = EFI_HII_PACKAGE_SIMPLE_FONTS;
1898 SimplifiedFont->NumberOfNarrowGlyphs = (UINT16) (mNarrowFontSize / sizeof (EFI_NARROW_GLYPH));
1899
1900 Location = (UINT8 *) (&SimplifiedFont->NumberOfWideGlyphs + 1);
1901 CopyMem (Location, gUsStdNarrowGlyphData, mNarrowFontSize);
1902
1903 //
1904 // Add this simplified font package to a package list then install it.
1905 //
1906 mHiiHandle = HiiAddPackages (
1907 &mFontPackageListGuid,
1908 NULL,
1909 Package,
1910 NULL
1911 );
1912 ASSERT (mHiiHandle != NULL);
1913 FreePool (Package);
1914 }
1915
1916 /**
1917 The user Entry Point for module GraphicsConsole. The user code starts with this function.
1918
1919 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1920 @param[in] SystemTable A pointer to the EFI System Table.
1921
1922 @retval EFI_SUCCESS The entry point is executed successfully.
1923 @return other Some error occurs when executing this entry point.
1924
1925 **/
1926 EFI_STATUS
1927 EFIAPI
1928 InitializeGraphicsConsole (
1929 IN EFI_HANDLE ImageHandle,
1930 IN EFI_SYSTEM_TABLE *SystemTable
1931 )
1932 {
1933 EFI_STATUS Status;
1934
1935 //
1936 // Register notify function on HII Database Protocol to add font package.
1937 //
1938 EfiCreateProtocolNotifyEvent (
1939 &gEfiHiiDatabaseProtocolGuid,
1940 TPL_CALLBACK,
1941 RegisterFontPackage,
1942 NULL,
1943 &mHiiRegistration
1944 );
1945
1946 //
1947 // Install driver model protocol(s).
1948 //
1949 Status = EfiLibInstallDriverBindingComponentName2 (
1950 ImageHandle,
1951 SystemTable,
1952 &gGraphicsConsoleDriverBinding,
1953 ImageHandle,
1954 &gGraphicsConsoleComponentName,
1955 &gGraphicsConsoleComponentName2
1956 );
1957 ASSERT_EFI_ERROR (Status);
1958
1959 return Status;
1960 }
1961
1962