+/**\r
+ This function will change video resolution and text mode for setup when setup is launched.\r
+\r
+ @param None.\r
+\r
+ @retval EFI_SUCCESS Mode is changed successfully.\r
+ @retval Others Mode failed to changed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ChangeModeForSetup (\r
+ VOID\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
+ UINTN HandleCount;\r
+ EFI_HANDLE *HandleBuffer;\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ UINTN CurrentColumn;\r
+ UINTN CurrentRow; \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
+ //\r
+ // Get user defined text mode for setup only once.\r
+ // \r
+ if (!mSetupModeInitialized) {\r
+ mSetupHorizontalResolution = PcdGet32 (PcdSetupVideoHorizontalResolution);\r
+ mSetupVerticalResolution = PcdGet32 (PcdSetupVideoVerticalResolution); \r
+ mSetupTextModeColumn = PcdGet32 (PcdSetupConOutColumn);\r
+ mSetupTextModeRow = PcdGet32 (PcdSetupConOutRow);\r
+ mSetupModeInitialized = TRUE;\r
+ }\r
+\r
+ MaxGopMode = GraphicsOutput->Mode->MaxMode;\r
+ MaxTextMode = SimpleTextOut->Mode->MaxMode;\r
+\r
+ //\r
+ // 1. If current video resolution is same with setup video resolution,\r
+ // video resolution need not be changed.\r
+ // 1.1. If current text mode is same with setup text mode, text mode need not be changed.\r
+ // 1.2. If current text mode is different with setup text mode, text mode need be changed to setup text mode.\r
+ // 2. If current video resolution is different with setup 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 == mSetupHorizontalResolution) &&\r
+ (Info->VerticalResolution == mSetupVerticalResolution)) {\r
+ if ((GraphicsOutput->Mode->Info->HorizontalResolution == mSetupHorizontalResolution) &&\r
+ (GraphicsOutput->Mode->Info->VerticalResolution == mSetupVerticalResolution)) {\r
+ //\r
+ // If current video resolution is same with setup video resolution, \r
+ // then check if current text mode is same with setup text mode.\r
+ //\r
+ Status = SimpleTextOut->QueryMode (SimpleTextOut, SimpleTextOut->Mode->Mode, &CurrentColumn, &CurrentRow);\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (CurrentColumn == mSetupTextModeColumn && CurrentRow == mSetupTextModeRow) {\r
+ //\r
+ // Current text mode is same with setup text mode, text mode need not be change.\r
+ //\r
+ FreePool (Info);\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ //\r
+ // Current text mode is different with setup text mode, text mode need be change to new text 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 == mSetupTextModeColumn) && (CurrentRow == mSetupTextModeRow)) {\r
+ //\r
+ // setup 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 setup text mode is not supported, return error.\r
+ //\r
+ FreePool (Info);\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ }\r
+ } else {\r
+ FreePool (Info);\r
+ //\r
+ // If current video resolution is not same with the setup video resolution, set new video resolution.\r
+ // In this case, the drivers which produce simple text out need be restarted.\r
+ //\r
+ Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Set PCD to restart GraphicsConsole and Consplitter to change video resolution \r
+ // and produce new text mode based on new resolution.\r
+ //\r
+ PcdSet32 (PcdVideoHorizontalResolution, mSetupHorizontalResolution);\r
+ PcdSet32 (PcdVideoVerticalResolution, mSetupVerticalResolution);\r
+ PcdSet32 (PcdConOutColumn, mSetupTextModeColumn);\r
+ PcdSet32 (PcdConOutRow, mSetupTextModeRow);\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
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ FreePool (Info);\r
+ }\r
+ }\r
+\r
+ if (ModeNumber == MaxGopMode) {\r
+ //\r
+ // If the new resolution is not supported, return error.\r
+ //\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r