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