]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/QemuVideoDxe/VbeShim.c
OvmfPkg/QemuVideoDxe/VbeShim: rename Status to Segment0AllocationStatus
[mirror_edk2.git] / OvmfPkg / QemuVideoDxe / VbeShim.c
CommitLineData
90803342
LE
1/** @file\r
2 Install a fake VGABIOS service handler (real mode Int10h) for the buggy\r
3 Windows 2008 R2 SP1 UEFI guest.\r
4\r
5 The handler is never meant to be directly executed by a VCPU; it's there for\r
6 the internal real mode emulator of Windows 2008 R2 SP1.\r
7\r
8 The code is based on Ralf Brown's Interrupt List:\r
9 <http://www.cs.cmu.edu/~ralf/files.html>\r
10 <http://www.ctyme.com/rbrown.htm>\r
11\r
12 Copyright (C) 2014, Red Hat, Inc.\r
13 Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
14\r
15 This program and the accompanying materials are licensed and made available\r
16 under the terms and conditions of the BSD License which accompanies this\r
17 distribution. The full text of the license may be found at\r
18 http://opensource.org/licenses/bsd-license.php\r
19\r
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
21 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
22**/\r
23\r
24#include <IndustryStandard/LegacyVgaBios.h>\r
25#include <Library/DebugLib.h>\r
26#include <Library/PciLib.h>\r
27#include <Library/PrintLib.h>\r
28\r
29#include "Qemu.h"\r
30#include "VbeShim.h"\r
31\r
32#pragma pack (1)\r
33typedef struct {\r
34 UINT16 Offset;\r
35 UINT16 Segment;\r
36} IVT_ENTRY;\r
37#pragma pack ()\r
38\r
39//\r
40// This string is displayed by Windows 2008 R2 SP1 in the Screen Resolution,\r
41// Advanced Settings dialog. It should be short.\r
42//\r
43STATIC CONST CHAR8 mProductRevision[] = "OVMF Int10h (fake)";\r
44\r
45/**\r
46 Install the VBE Info and VBE Mode Info structures, and the VBE service\r
47 handler routine in the C segment. Point the real-mode Int10h interrupt vector\r
48 to the handler. The only advertised mode is 1024x768x32.\r
49\r
50 @param[in] CardName Name of the video card to be exposed in the\r
51 Product Name field of the VBE Info structure. The\r
52 parameter must originate from a\r
53 QEMU_VIDEO_CARD.Name field.\r
54 @param[in] FrameBufferBase Guest-physical base address of the video card's\r
55 frame buffer.\r
56**/\r
57VOID\r
58InstallVbeShim (\r
59 IN CONST CHAR16 *CardName,\r
60 IN EFI_PHYSICAL_ADDRESS FrameBufferBase\r
61 )\r
62{\r
63 EFI_PHYSICAL_ADDRESS Segment0, SegmentC, SegmentF;\r
64 UINTN Segment0Pages;\r
65 IVT_ENTRY *Int0x10;\r
ce461ae2 66 EFI_STATUS Segment0AllocationStatus;\r
90803342
LE
67 UINTN Pam1Address;\r
68 UINT8 Pam1;\r
69 UINTN SegmentCPages;\r
70 VBE_INFO *VbeInfoFull;\r
71 VBE_INFO_BASE *VbeInfo;\r
72 UINT8 *Ptr;\r
73 UINTN Printed;\r
74 VBE_MODE_INFO *VbeModeInfo;\r
75\r
76 Segment0 = 0x00000;\r
77 SegmentC = 0xC0000;\r
78 SegmentF = 0xF0000;\r
79\r
80 //\r
81 // Attempt to cover the real mode IVT with an allocation. This is a UEFI\r
82 // driver, hence the arch protocols have been installed previously. Among\r
83 // those, the CPU arch protocol has configured the IDT, so we can overwrite\r
84 // the IVT used in real mode.\r
85 //\r
86 // The allocation request may fail, eg. if LegacyBiosDxe has already run.\r
87 //\r
88 Segment0Pages = 1;\r
89 Int0x10 = (IVT_ENTRY *)(UINTN)Segment0 + 0x10;\r
ce461ae2
LE
90 Segment0AllocationStatus = gBS->AllocatePages (\r
91 AllocateAddress,\r
92 EfiBootServicesCode,\r
93 Segment0Pages,\r
94 &Segment0\r
95 );\r
96\r
97 if (EFI_ERROR (Segment0AllocationStatus)) {\r
90803342
LE
98 EFI_PHYSICAL_ADDRESS Handler;\r
99\r
100 //\r
101 // Check if a video BIOS handler has been installed previously -- we\r
102 // shouldn't override a real video BIOS with our shim, nor our own shim if\r
103 // it's already present.\r
104 //\r
105 Handler = (Int0x10->Segment << 4) + Int0x10->Offset;\r
106 if (Handler >= SegmentC && Handler < SegmentF) {\r
4dd8787a 107 DEBUG ((EFI_D_INFO, "%a: Video BIOS handler found at %04x:%04x\n",\r
90803342
LE
108 __FUNCTION__, Int0x10->Segment, Int0x10->Offset));\r
109 return;\r
110 }\r
111\r
112 //\r
113 // Otherwise we'll overwrite the Int10h vector, even though we may not own\r
114 // the page at zero.\r
115 //\r
ce461ae2
LE
116 DEBUG ((\r
117 DEBUG_INFO,\r
118 "%a: failed to allocate page at zero: %r\n",\r
119 __FUNCTION__,\r
120 Segment0AllocationStatus\r
121 ));\r
90803342
LE
122 } else {\r
123 //\r
124 // We managed to allocate the page at zero. SVN r14218 guarantees that it\r
125 // is NUL-filled.\r
126 //\r
127 ASSERT (Int0x10->Segment == 0x0000);\r
128 ASSERT (Int0x10->Offset == 0x0000);\r
129 }\r
130\r
131 //\r
132 // Put the shim in place first.\r
133 //\r
134 Pam1Address = PCI_LIB_ADDRESS (0, 0, 0, 0x5A);\r
135 //\r
136 // low nibble covers 0xC0000 to 0xC3FFF\r
137 // high nibble covers 0xC4000 to 0xC7FFF\r
138 // bit1 in each nibble is Write Enable\r
139 // bit0 in each nibble is Read Enable\r
140 //\r
141 Pam1 = PciRead8 (Pam1Address);\r
142 PciWrite8 (Pam1Address, Pam1 | (BIT1 | BIT0));\r
143\r
144 //\r
8c0b0b34 145 // We never added memory space during PEI or DXE for the C segment, so we\r
90803342
LE
146 // don't need to (and can't) allocate from there. Also, guest operating\r
147 // systems will see a hole in the UEFI memory map there.\r
148 //\r
149 SegmentCPages = 4;\r
150\r
151 ASSERT (sizeof mVbeShim <= EFI_PAGES_TO_SIZE (SegmentCPages));\r
152 CopyMem ((VOID *)(UINTN)SegmentC, mVbeShim, sizeof mVbeShim);\r
153\r
154 //\r
155 // Fill in the VBE INFO structure.\r
156 //\r
157 VbeInfoFull = (VBE_INFO *)(UINTN)SegmentC;\r
158 VbeInfo = &VbeInfoFull->Base;\r
159 Ptr = VbeInfoFull->Buffer;\r
160\r
161 CopyMem (VbeInfo->Signature, "VESA", 4);\r
162 VbeInfo->VesaVersion = 0x0300;\r
163\r
75f8e3aa 164 VbeInfo->OemNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
165 CopyMem (Ptr, "QEMU", 5);\r
166 Ptr += 5;\r
167\r
168 VbeInfo->Capabilities = BIT0; // DAC can be switched into 8-bit mode\r
169\r
75f8e3aa 170 VbeInfo->ModeListAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
171 *(UINT16*)Ptr = 0x00f1; // mode number\r
172 Ptr += 2;\r
173 *(UINT16*)Ptr = 0xFFFF; // mode list terminator\r
174 Ptr += 2;\r
175\r
176 VbeInfo->VideoMem64K = (UINT16)((1024 * 768 * 4 + 65535) / 65536);\r
177 VbeInfo->OemSoftwareVersion = 0x0000;\r
178\r
75f8e3aa 179 VbeInfo->VendorNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
180 CopyMem (Ptr, "OVMF", 5);\r
181 Ptr += 5;\r
182\r
75f8e3aa 183 VbeInfo->ProductNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
184 Printed = AsciiSPrint ((CHAR8 *)Ptr,\r
185 sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer), "%s",\r
186 CardName);\r
187 Ptr += Printed + 1;\r
188\r
75f8e3aa 189 VbeInfo->ProductRevAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
190 CopyMem (Ptr, mProductRevision, sizeof mProductRevision);\r
191 Ptr += sizeof mProductRevision;\r
192\r
193 ASSERT (sizeof VbeInfoFull->Buffer >= Ptr - VbeInfoFull->Buffer);\r
194 ZeroMem (Ptr, sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer));\r
195\r
196 //\r
197 // Fil in the VBE MODE INFO structure.\r
198 //\r
199 VbeModeInfo = (VBE_MODE_INFO *)(VbeInfoFull + 1);\r
200\r
201 //\r
202 // bit0: mode supported by present hardware configuration\r
203 // bit1: optional information available (must be =1 for VBE v1.2+)\r
204 // bit3: set if color, clear if monochrome\r
205 // bit4: set if graphics mode, clear if text mode\r
206 // bit5: mode is not VGA-compatible\r
207 // bit7: linear framebuffer mode supported\r
208 //\r
209 VbeModeInfo->ModeAttr = BIT7 | BIT5 | BIT4 | BIT3 | BIT1 | BIT0;\r
210\r
211 //\r
212 // bit0: exists\r
213 // bit1: bit1: readable\r
214 // bit2: writeable\r
215 //\r
216 VbeModeInfo->WindowAAttr = BIT2 | BIT1 | BIT0;\r
217\r
218 VbeModeInfo->WindowBAttr = 0x00;\r
219 VbeModeInfo->WindowGranularityKB = 0x0040;\r
220 VbeModeInfo->WindowSizeKB = 0x0040;\r
221 VbeModeInfo->WindowAStartSegment = 0xA000;\r
222 VbeModeInfo->WindowBStartSegment = 0x0000;\r
223 VbeModeInfo->WindowPositioningAddress = 0x0000;\r
224 VbeModeInfo->BytesPerScanLine = 1024 * 4;\r
225\r
226 VbeModeInfo->Width = 1024;\r
227 VbeModeInfo->Height = 768;\r
228 VbeModeInfo->CharCellWidth = 8;\r
229 VbeModeInfo->CharCellHeight = 16;\r
230 VbeModeInfo->NumPlanes = 1;\r
231 VbeModeInfo->BitsPerPixel = 32;\r
232 VbeModeInfo->NumBanks = 1;\r
233 VbeModeInfo->MemoryModel = 6; // direct color\r
234 VbeModeInfo->BankSizeKB = 0;\r
235 VbeModeInfo->NumImagePagesLessOne = 0;\r
236 VbeModeInfo->Vbe3 = 0x01;\r
237\r
238 VbeModeInfo->RedMaskSize = 8;\r
239 VbeModeInfo->RedMaskPos = 16;\r
240 VbeModeInfo->GreenMaskSize = 8;\r
241 VbeModeInfo->GreenMaskPos = 8;\r
242 VbeModeInfo->BlueMaskSize = 8;\r
243 VbeModeInfo->BlueMaskPos = 0;\r
244 VbeModeInfo->ReservedMaskSize = 8;\r
245 VbeModeInfo->ReservedMaskPos = 24;\r
246\r
247 //\r
248 // bit1: Bytes in reserved field may be used by application\r
249 //\r
250 VbeModeInfo->DirectColorModeInfo = BIT1;\r
251\r
252 VbeModeInfo->LfbAddress = (UINT32)FrameBufferBase;\r
253 VbeModeInfo->OffScreenAddress = 0;\r
254 VbeModeInfo->OffScreenSizeKB = 0;\r
255\r
256 VbeModeInfo->BytesPerScanLineLinear = 1024 * 4;\r
257 VbeModeInfo->NumImagesLessOneBanked = 0;\r
258 VbeModeInfo->NumImagesLessOneLinear = 0;\r
259 VbeModeInfo->RedMaskSizeLinear = 8;\r
260 VbeModeInfo->RedMaskPosLinear = 16;\r
261 VbeModeInfo->GreenMaskSizeLinear = 8;\r
262 VbeModeInfo->GreenMaskPosLinear = 8;\r
263 VbeModeInfo->BlueMaskSizeLinear = 8;\r
264 VbeModeInfo->BlueMaskPosLinear = 0;\r
265 VbeModeInfo->ReservedMaskSizeLinear = 8;\r
266 VbeModeInfo->ReservedMaskPosLinear = 24;\r
267 VbeModeInfo->MaxPixelClockHz = 0;\r
268\r
269 ZeroMem (VbeModeInfo->Reserved, sizeof VbeModeInfo->Reserved);\r
270\r
271 //\r
272 // Clear Write Enable (bit1), keep Read Enable (bit0) set\r
273 //\r
274 PciWrite8 (Pam1Address, (Pam1 & ~BIT1) | BIT0);\r
275\r
276 //\r
277 // Second, point the Int10h vector at the shim.\r
278 //\r
75f8e3aa 279 Int0x10->Segment = (UINT16) ((UINT32)SegmentC >> 4);\r
ea5396f3 280 Int0x10->Offset = (UINT16) ((UINTN) (VbeModeInfo + 1) - SegmentC);\r
90803342
LE
281\r
282 DEBUG ((EFI_D_INFO, "%a: VBE shim installed\n", __FUNCTION__));\r
283}\r