]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/QemuVideoDxe/VbeShim.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
ac0a286f
MK
29 UINT16 Offset;\r
30 UINT16 Segment;\r
90803342
LE
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
ac0a286f 38STATIC CONST CHAR8 mProductRevision[] = "OVMF Int10h (fake)";\r
90803342
LE
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
ac0a286f
MK
54 IN CONST CHAR16 *CardName,\r
55 IN EFI_PHYSICAL_ADDRESS FrameBufferBase\r
90803342
LE
56 )\r
57{\r
ac0a286f
MK
58 EFI_PHYSICAL_ADDRESS Segment0, SegmentC, SegmentF;\r
59 UINTN Segment0Pages;\r
60 IVT_ENTRY *Int0x10;\r
61 EFI_STATUS Segment0AllocationStatus;\r
62 UINT16 HostBridgeDevId;\r
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
90803342 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
ac0a286f
MK
98 Segment0Pages = 1;\r
99 Int0x10 = (IVT_ENTRY *)(UINTN)(Segment0 + 0x10 * sizeof (IVT_ENTRY));\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
ac0a286f 108 EFI_PHYSICAL_ADDRESS Handler;\r
90803342
LE
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
ac0a286f
MK
116 if ((Handler >= SegmentC) && (Handler < SegmentF)) {\r
117 DEBUG ((\r
118 DEBUG_INFO,\r
119 "%a: Video BIOS handler found at %04x:%04x\n",\r
120 __FUNCTION__,\r
121 Int0x10->Segment,\r
122 Int0x10->Offset\r
123 ));\r
90803342
LE
124 return;\r
125 }\r
126\r
127 //\r
128 // Otherwise we'll overwrite the Int10h vector, even though we may not own\r
129 // the page at zero.\r
130 //\r
ce461ae2
LE
131 DEBUG ((\r
132 DEBUG_INFO,\r
133 "%a: failed to allocate page at zero: %r\n",\r
134 __FUNCTION__,\r
135 Segment0AllocationStatus\r
136 ));\r
90803342
LE
137 } else {\r
138 //\r
139 // We managed to allocate the page at zero. SVN r14218 guarantees that it\r
140 // is NUL-filled.\r
141 //\r
142 ASSERT (Int0x10->Segment == 0x0000);\r
143 ASSERT (Int0x10->Offset == 0x0000);\r
144 }\r
145\r
146 //\r
147 // Put the shim in place first.\r
148 //\r
947f3737
LE
149 // Start by determining the address of the PAM1 register.\r
150 //\r
151 HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);\r
152 switch (HostBridgeDevId) {\r
ac0a286f
MK
153 case INTEL_82441_DEVICE_ID:\r
154 Pam1Address = PMC_REGISTER_PIIX4 (PIIX4_PAM1);\r
155 break;\r
156 case INTEL_Q35_MCH_DEVICE_ID:\r
157 Pam1Address = DRAMC_REGISTER_Q35 (MCH_PAM1);\r
158 break;\r
ad3bafa7
GH
159 case MICROVM_PSEUDO_DEVICE_ID:\r
160 return;\r
ac0a286f
MK
161 default:\r
162 DEBUG ((\r
163 DEBUG_ERROR,\r
164 "%a: unknown host bridge device ID: 0x%04x\n",\r
165 __FUNCTION__,\r
166 HostBridgeDevId\r
167 ));\r
168 ASSERT (FALSE);\r
169\r
170 if (!EFI_ERROR (Segment0AllocationStatus)) {\r
171 gBS->FreePages (Segment0, Segment0Pages);\r
172 }\r
947f3737 173\r
ac0a286f 174 return;\r
947f3737 175 }\r
ac0a286f 176\r
90803342
LE
177 //\r
178 // low nibble covers 0xC0000 to 0xC3FFF\r
179 // high nibble covers 0xC4000 to 0xC7FFF\r
180 // bit1 in each nibble is Write Enable\r
181 // bit0 in each nibble is Read Enable\r
182 //\r
183 Pam1 = PciRead8 (Pam1Address);\r
184 PciWrite8 (Pam1Address, Pam1 | (BIT1 | BIT0));\r
185\r
186 //\r
8c0b0b34 187 // We never added memory space during PEI or DXE for the C segment, so we\r
90803342
LE
188 // don't need to (and can't) allocate from there. Also, guest operating\r
189 // systems will see a hole in the UEFI memory map there.\r
190 //\r
191 SegmentCPages = 4;\r
192\r
193 ASSERT (sizeof mVbeShim <= EFI_PAGES_TO_SIZE (SegmentCPages));\r
194 CopyMem ((VOID *)(UINTN)SegmentC, mVbeShim, sizeof mVbeShim);\r
195\r
196 //\r
197 // Fill in the VBE INFO structure.\r
198 //\r
199 VbeInfoFull = (VBE_INFO *)(UINTN)SegmentC;\r
200 VbeInfo = &VbeInfoFull->Base;\r
201 Ptr = VbeInfoFull->Buffer;\r
202\r
203 CopyMem (VbeInfo->Signature, "VESA", 4);\r
204 VbeInfo->VesaVersion = 0x0300;\r
205\r
75f8e3aa 206 VbeInfo->OemNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
207 CopyMem (Ptr, "QEMU", 5);\r
208 Ptr += 5;\r
209\r
210 VbeInfo->Capabilities = BIT0; // DAC can be switched into 8-bit mode\r
211\r
75f8e3aa 212 VbeInfo->ModeListAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
ac0a286f
MK
213 *(UINT16 *)Ptr = 0x00f1; // mode number\r
214 Ptr += 2;\r
215 *(UINT16 *)Ptr = 0xFFFF; // mode list terminator\r
216 Ptr += 2;\r
90803342 217\r
ac0a286f 218 VbeInfo->VideoMem64K = (UINT16)((1024 * 768 * 4 + 65535) / 65536);\r
90803342
LE
219 VbeInfo->OemSoftwareVersion = 0x0000;\r
220\r
75f8e3aa 221 VbeInfo->VendorNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
222 CopyMem (Ptr, "OVMF", 5);\r
223 Ptr += 5;\r
224\r
75f8e3aa 225 VbeInfo->ProductNameAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
ac0a286f
MK
226 Printed = AsciiSPrint (\r
227 (CHAR8 *)Ptr,\r
228 sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer),\r
229 "%s",\r
230 CardName\r
231 );\r
90803342
LE
232 Ptr += Printed + 1;\r
233\r
75f8e3aa 234 VbeInfo->ProductRevAddress = (UINT32)SegmentC << 12 | (UINT16)(UINTN)Ptr;\r
90803342
LE
235 CopyMem (Ptr, mProductRevision, sizeof mProductRevision);\r
236 Ptr += sizeof mProductRevision;\r
237\r
238 ASSERT (sizeof VbeInfoFull->Buffer >= Ptr - VbeInfoFull->Buffer);\r
239 ZeroMem (Ptr, sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer));\r
240\r
241 //\r
242 // Fil in the VBE MODE INFO structure.\r
243 //\r
244 VbeModeInfo = (VBE_MODE_INFO *)(VbeInfoFull + 1);\r
245\r
246 //\r
247 // bit0: mode supported by present hardware configuration\r
248 // bit1: optional information available (must be =1 for VBE v1.2+)\r
249 // bit3: set if color, clear if monochrome\r
250 // bit4: set if graphics mode, clear if text mode\r
251 // bit5: mode is not VGA-compatible\r
252 // bit7: linear framebuffer mode supported\r
253 //\r
254 VbeModeInfo->ModeAttr = BIT7 | BIT5 | BIT4 | BIT3 | BIT1 | BIT0;\r
255\r
256 //\r
257 // bit0: exists\r
258 // bit1: bit1: readable\r
259 // bit2: writeable\r
260 //\r
ac0a286f 261 VbeModeInfo->WindowAAttr = BIT2 | BIT1 | BIT0;\r
90803342
LE
262\r
263 VbeModeInfo->WindowBAttr = 0x00;\r
264 VbeModeInfo->WindowGranularityKB = 0x0040;\r
265 VbeModeInfo->WindowSizeKB = 0x0040;\r
266 VbeModeInfo->WindowAStartSegment = 0xA000;\r
267 VbeModeInfo->WindowBStartSegment = 0x0000;\r
268 VbeModeInfo->WindowPositioningAddress = 0x0000;\r
269 VbeModeInfo->BytesPerScanLine = 1024 * 4;\r
270\r
271 VbeModeInfo->Width = 1024;\r
272 VbeModeInfo->Height = 768;\r
273 VbeModeInfo->CharCellWidth = 8;\r
274 VbeModeInfo->CharCellHeight = 16;\r
275 VbeModeInfo->NumPlanes = 1;\r
276 VbeModeInfo->BitsPerPixel = 32;\r
277 VbeModeInfo->NumBanks = 1;\r
278 VbeModeInfo->MemoryModel = 6; // direct color\r
279 VbeModeInfo->BankSizeKB = 0;\r
280 VbeModeInfo->NumImagePagesLessOne = 0;\r
281 VbeModeInfo->Vbe3 = 0x01;\r
282\r
283 VbeModeInfo->RedMaskSize = 8;\r
284 VbeModeInfo->RedMaskPos = 16;\r
285 VbeModeInfo->GreenMaskSize = 8;\r
286 VbeModeInfo->GreenMaskPos = 8;\r
287 VbeModeInfo->BlueMaskSize = 8;\r
288 VbeModeInfo->BlueMaskPos = 0;\r
289 VbeModeInfo->ReservedMaskSize = 8;\r
290 VbeModeInfo->ReservedMaskPos = 24;\r
291\r
292 //\r
293 // bit1: Bytes in reserved field may be used by application\r
294 //\r
295 VbeModeInfo->DirectColorModeInfo = BIT1;\r
296\r
297 VbeModeInfo->LfbAddress = (UINT32)FrameBufferBase;\r
298 VbeModeInfo->OffScreenAddress = 0;\r
299 VbeModeInfo->OffScreenSizeKB = 0;\r
300\r
301 VbeModeInfo->BytesPerScanLineLinear = 1024 * 4;\r
302 VbeModeInfo->NumImagesLessOneBanked = 0;\r
303 VbeModeInfo->NumImagesLessOneLinear = 0;\r
304 VbeModeInfo->RedMaskSizeLinear = 8;\r
305 VbeModeInfo->RedMaskPosLinear = 16;\r
306 VbeModeInfo->GreenMaskSizeLinear = 8;\r
307 VbeModeInfo->GreenMaskPosLinear = 8;\r
308 VbeModeInfo->BlueMaskSizeLinear = 8;\r
309 VbeModeInfo->BlueMaskPosLinear = 0;\r
310 VbeModeInfo->ReservedMaskSizeLinear = 8;\r
311 VbeModeInfo->ReservedMaskPosLinear = 24;\r
312 VbeModeInfo->MaxPixelClockHz = 0;\r
313\r
314 ZeroMem (VbeModeInfo->Reserved, sizeof VbeModeInfo->Reserved);\r
315\r
316 //\r
317 // Clear Write Enable (bit1), keep Read Enable (bit0) set\r
318 //\r
319 PciWrite8 (Pam1Address, (Pam1 & ~BIT1) | BIT0);\r
320\r
321 //\r
322 // Second, point the Int10h vector at the shim.\r
323 //\r
ac0a286f
MK
324 Int0x10->Segment = (UINT16)((UINT32)SegmentC >> 4);\r
325 Int0x10->Offset = (UINT16)((UINTN)(VbeModeInfo + 1) - SegmentC);\r
90803342 326\r
70d5086c 327 DEBUG ((DEBUG_INFO, "%a: VBE shim installed\n", __FUNCTION__));\r
90803342 328}\r