+\r
+/**\r
+ This function will change video resolution and text mode\r
+ according to defined setup mode or defined boot mode \r
+\r
+ @param IsSetupMode Indicate mode is changed to setup mode or boot mode. \r
+\r
+ @retval EFI_SUCCESS Mode is changed successfully.\r
+ @retval Others Mode failed to be changed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsSetConsoleMode (\r
+ BOOLEAN IsSetupMode\r
+ )\r
+{\r
+ EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;\r
+ EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut;\r
+ UINTN SizeOfInfo;\r
+ EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
+ UINT32 MaxGopMode;\r
+ UINT32 MaxTextMode;\r
+ UINT32 ModeNumber;\r
+ UINT32 NewHorizontalResolution;\r
+ UINT32 NewVerticalResolution;\r
+ UINT32 NewColumns;\r
+ UINT32 NewRows;\r
+ UINTN HandleCount;\r
+ EFI_HANDLE *HandleBuffer;\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ UINTN CurrentColumn;\r
+ UINTN CurrentRow; \r
+\r
+ MaxGopMode = 0;\r
+ MaxTextMode = 0;\r
+\r
+ //\r
+ // Get current video resolution and text mode \r
+ //\r
+ Status = gBS->HandleProtocol (\r
+ gST->ConsoleOutHandle,\r
+ &gEfiGraphicsOutputProtocolGuid,\r
+ (VOID**)&GraphicsOutput\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ GraphicsOutput = NULL;\r
+ }\r
+\r
+ Status = gBS->HandleProtocol (\r
+ gST->ConsoleOutHandle,\r
+ &gEfiSimpleTextOutProtocolGuid,\r
+ (VOID**)&SimpleTextOut\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ SimpleTextOut = NULL;\r
+ } \r
+\r
+ if ((GraphicsOutput == NULL) || (SimpleTextOut == NULL)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ if (IsSetupMode) {\r
+ //\r
+ // The requried resolution and text mode is setup mode.\r
+ //\r
+ NewHorizontalResolution = mSetupHorizontalResolution;\r
+ NewVerticalResolution = mSetupVerticalResolution;\r
+ NewColumns = mSetupTextModeColumn;\r
+ NewRows = mSetupTextModeRow;\r
+ } else {\r
+ //\r
+ // The required resolution and text mode is boot mode.\r
+ //\r
+ NewHorizontalResolution = mBootHorizontalResolution;\r
+ NewVerticalResolution = mBootVerticalResolution;\r
+ NewColumns = mBootTextModeColumn;\r
+ NewRows = mBootTextModeRow; \r
+ }\r
+ \r
+ if (GraphicsOutput != NULL) {\r
+ MaxGopMode = GraphicsOutput->Mode->MaxMode;\r
+ } \r
+\r
+ if (SimpleTextOut != NULL) {\r
+ MaxTextMode = SimpleTextOut->Mode->MaxMode;\r
+ }\r
+\r
+ //\r
+ // 1. If current video resolution is same with required video resolution,\r
+ // video resolution need not be changed.\r
+ // 1.1. If current text mode is same with required text mode, text mode need not be changed.\r
+ // 1.2. If current text mode is different from required text mode, text mode need be changed.\r
+ // 2. If current video resolution is different from required video resolution, we need restart whole console drivers.\r
+ //\r
+ for (ModeNumber = 0; ModeNumber < MaxGopMode; ModeNumber++) {\r
+ Status = GraphicsOutput->QueryMode (\r
+ GraphicsOutput,\r
+ ModeNumber,\r
+ &SizeOfInfo,\r
+ &Info\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ if ((Info->HorizontalResolution == NewHorizontalResolution) &&\r
+ (Info->VerticalResolution == NewVerticalResolution)) {\r
+ if ((GraphicsOutput->Mode->Info->HorizontalResolution == NewHorizontalResolution) &&\r
+ (GraphicsOutput->Mode->Info->VerticalResolution == NewVerticalResolution)) {\r
+ //\r
+ // Current resolution is same with required resolution, check if text mode need be set\r
+ //\r
+ Status = SimpleTextOut->QueryMode (SimpleTextOut, SimpleTextOut->Mode->Mode, &CurrentColumn, &CurrentRow);\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (CurrentColumn == NewColumns && CurrentRow == NewRows) {\r
+ //\r
+ // If current text mode is same with required text mode. Do nothing\r
+ //\r
+ FreePool (Info);\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ //\r
+ // If current text mode is different from requried text mode. Set new video mode\r
+ //\r
+ for (Index = 0; Index < MaxTextMode; Index++) {\r
+ Status = SimpleTextOut->QueryMode (SimpleTextOut, Index, &CurrentColumn, &CurrentRow);\r
+ if (!EFI_ERROR(Status)) {\r
+ if ((CurrentColumn == NewColumns) && (CurrentRow == NewRows)) {\r
+ //\r
+ // Required text mode is supported, set it.\r
+ //\r
+ Status = SimpleTextOut->SetMode (SimpleTextOut, Index);\r
+ ASSERT_EFI_ERROR (Status);\r
+ //\r
+ // Update text mode PCD.\r
+ //\r
+ PcdSet32 (PcdConOutColumn, mSetupTextModeColumn);\r
+ PcdSet32 (PcdConOutRow, mSetupTextModeRow);\r
+ FreePool (Info);\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+ if (Index == MaxTextMode) {\r
+ //\r
+ // If requried text mode is not supported, return error.\r
+ //\r
+ FreePool (Info);\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ }\r
+ } else {\r
+ //\r
+ // If current video resolution is not same with the new one, set new video resolution.\r
+ // In this case, the driver which produces simple text out need be restarted.\r
+ //\r
+ Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
+ if (!EFI_ERROR (Status)) {\r
+ FreePool (Info);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ FreePool (Info);\r
+ }\r
+ }\r
+\r
+ if (ModeNumber == MaxGopMode) {\r
+ //\r
+ // If the resolution is not supported, return error.\r
+ //\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ //\r
+ // Set PCD to Inform GraphicsConsole to change video resolution.\r
+ // Set PCD to Inform Consplitter to change text mode.\r
+ //\r
+ PcdSet32 (PcdVideoHorizontalResolution, NewHorizontalResolution);\r
+ PcdSet32 (PcdVideoVerticalResolution, NewVerticalResolution);\r
+ PcdSet32 (PcdConOutColumn, NewColumns);\r
+ PcdSet32 (PcdConOutRow, NewRows);\r
+ \r
+ \r
+ //\r
+ // Video mode is changed, so restart graphics console driver and higher level driver.\r
+ // Reconnect graphics console driver and higher level driver.\r
+ // Locate all the handles with GOP protocol and reconnect it.\r
+ //\r
+ Status = gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiSimpleTextOutProtocolGuid,\r
+ NULL,\r
+ &HandleCount,\r
+ &HandleBuffer\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ for (Index = 0; Index < HandleCount; Index++) {\r
+ gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
+ }\r
+ for (Index = 0; Index < HandleCount; Index++) {\r
+ gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
+ }\r
+ if (HandleBuffer != NULL) {\r
+ FreePool (HandleBuffer);\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r