]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/QemuVideoDxe/VbeShim.c
OvmfPkg: flash driver: drop needlessly wide multiplication (VS2010)
[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
66 EFI_STATUS Status;\r
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
90 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode,\r
91 Segment0Pages, &Segment0);\r
92\r
93 if (EFI_ERROR (Status)) {\r
94 EFI_PHYSICAL_ADDRESS Handler;\r
95\r
96 //\r
97 // Check if a video BIOS handler has been installed previously -- we\r
98 // shouldn't override a real video BIOS with our shim, nor our own shim if\r
99 // it's already present.\r
100 //\r
101 Handler = (Int0x10->Segment << 4) + Int0x10->Offset;\r
102 if (Handler >= SegmentC && Handler < SegmentF) {\r
103 DEBUG ((EFI_D_VERBOSE, "%a: Video BIOS handler found at %04x:%04x\n",\r
104 __FUNCTION__, Int0x10->Segment, Int0x10->Offset));\r
105 return;\r
106 }\r
107\r
108 //\r
109 // Otherwise we'll overwrite the Int10h vector, even though we may not own\r
110 // the page at zero.\r
111 //\r
112 DEBUG ((EFI_D_VERBOSE, "%a: failed to allocate page at zero: %r\n",\r
113 __FUNCTION__, Status));\r
114 } else {\r
115 //\r
116 // We managed to allocate the page at zero. SVN r14218 guarantees that it\r
117 // is NUL-filled.\r
118 //\r
119 ASSERT (Int0x10->Segment == 0x0000);\r
120 ASSERT (Int0x10->Offset == 0x0000);\r
121 }\r
122\r
123 //\r
124 // Put the shim in place first.\r
125 //\r
126 Pam1Address = PCI_LIB_ADDRESS (0, 0, 0, 0x5A);\r
127 //\r
128 // low nibble covers 0xC0000 to 0xC3FFF\r
129 // high nibble covers 0xC4000 to 0xC7FFF\r
130 // bit1 in each nibble is Write Enable\r
131 // bit0 in each nibble is Read Enable\r
132 //\r
133 Pam1 = PciRead8 (Pam1Address);\r
134 PciWrite8 (Pam1Address, Pam1 | (BIT1 | BIT0));\r
135\r
136 //\r
137 // We never added memory space durig PEI or DXE for the C segment, so we\r
138 // don't need to (and can't) allocate from there. Also, guest operating\r
139 // systems will see a hole in the UEFI memory map there.\r
140 //\r
141 SegmentCPages = 4;\r
142\r
143 ASSERT (sizeof mVbeShim <= EFI_PAGES_TO_SIZE (SegmentCPages));\r
144 CopyMem ((VOID *)(UINTN)SegmentC, mVbeShim, sizeof mVbeShim);\r
145\r
146 //\r
147 // Fill in the VBE INFO structure.\r
148 //\r
149 VbeInfoFull = (VBE_INFO *)(UINTN)SegmentC;\r
150 VbeInfo = &VbeInfoFull->Base;\r
151 Ptr = VbeInfoFull->Buffer;\r
152\r
153 CopyMem (VbeInfo->Signature, "VESA", 4);\r
154 VbeInfo->VesaVersion = 0x0300;\r
155\r
156 VbeInfo->OemNameAddress = (UINT32)(SegmentC << 12 | (UINT16)(UINTN)Ptr);\r
157 CopyMem (Ptr, "QEMU", 5);\r
158 Ptr += 5;\r
159\r
160 VbeInfo->Capabilities = BIT0; // DAC can be switched into 8-bit mode\r
161\r
162 VbeInfo->ModeListAddress = (UINT32)(SegmentC << 12 | (UINT16)(UINTN)Ptr);\r
163 *(UINT16*)Ptr = 0x00f1; // mode number\r
164 Ptr += 2;\r
165 *(UINT16*)Ptr = 0xFFFF; // mode list terminator\r
166 Ptr += 2;\r
167\r
168 VbeInfo->VideoMem64K = (UINT16)((1024 * 768 * 4 + 65535) / 65536);\r
169 VbeInfo->OemSoftwareVersion = 0x0000;\r
170\r
171 VbeInfo->VendorNameAddress = (UINT32)(SegmentC << 12 | (UINT16)(UINTN)Ptr);\r
172 CopyMem (Ptr, "OVMF", 5);\r
173 Ptr += 5;\r
174\r
175 VbeInfo->ProductNameAddress = (UINT32)(SegmentC << 12 | (UINT16)(UINTN)Ptr);\r
176 Printed = AsciiSPrint ((CHAR8 *)Ptr,\r
177 sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer), "%s",\r
178 CardName);\r
179 Ptr += Printed + 1;\r
180\r
181 VbeInfo->ProductRevAddress = (UINT32)(SegmentC << 12 | (UINT16)(UINTN)Ptr);\r
182 CopyMem (Ptr, mProductRevision, sizeof mProductRevision);\r
183 Ptr += sizeof mProductRevision;\r
184\r
185 ASSERT (sizeof VbeInfoFull->Buffer >= Ptr - VbeInfoFull->Buffer);\r
186 ZeroMem (Ptr, sizeof VbeInfoFull->Buffer - (Ptr - VbeInfoFull->Buffer));\r
187\r
188 //\r
189 // Fil in the VBE MODE INFO structure.\r
190 //\r
191 VbeModeInfo = (VBE_MODE_INFO *)(VbeInfoFull + 1);\r
192\r
193 //\r
194 // bit0: mode supported by present hardware configuration\r
195 // bit1: optional information available (must be =1 for VBE v1.2+)\r
196 // bit3: set if color, clear if monochrome\r
197 // bit4: set if graphics mode, clear if text mode\r
198 // bit5: mode is not VGA-compatible\r
199 // bit7: linear framebuffer mode supported\r
200 //\r
201 VbeModeInfo->ModeAttr = BIT7 | BIT5 | BIT4 | BIT3 | BIT1 | BIT0;\r
202\r
203 //\r
204 // bit0: exists\r
205 // bit1: bit1: readable\r
206 // bit2: writeable\r
207 //\r
208 VbeModeInfo->WindowAAttr = BIT2 | BIT1 | BIT0;\r
209\r
210 VbeModeInfo->WindowBAttr = 0x00;\r
211 VbeModeInfo->WindowGranularityKB = 0x0040;\r
212 VbeModeInfo->WindowSizeKB = 0x0040;\r
213 VbeModeInfo->WindowAStartSegment = 0xA000;\r
214 VbeModeInfo->WindowBStartSegment = 0x0000;\r
215 VbeModeInfo->WindowPositioningAddress = 0x0000;\r
216 VbeModeInfo->BytesPerScanLine = 1024 * 4;\r
217\r
218 VbeModeInfo->Width = 1024;\r
219 VbeModeInfo->Height = 768;\r
220 VbeModeInfo->CharCellWidth = 8;\r
221 VbeModeInfo->CharCellHeight = 16;\r
222 VbeModeInfo->NumPlanes = 1;\r
223 VbeModeInfo->BitsPerPixel = 32;\r
224 VbeModeInfo->NumBanks = 1;\r
225 VbeModeInfo->MemoryModel = 6; // direct color\r
226 VbeModeInfo->BankSizeKB = 0;\r
227 VbeModeInfo->NumImagePagesLessOne = 0;\r
228 VbeModeInfo->Vbe3 = 0x01;\r
229\r
230 VbeModeInfo->RedMaskSize = 8;\r
231 VbeModeInfo->RedMaskPos = 16;\r
232 VbeModeInfo->GreenMaskSize = 8;\r
233 VbeModeInfo->GreenMaskPos = 8;\r
234 VbeModeInfo->BlueMaskSize = 8;\r
235 VbeModeInfo->BlueMaskPos = 0;\r
236 VbeModeInfo->ReservedMaskSize = 8;\r
237 VbeModeInfo->ReservedMaskPos = 24;\r
238\r
239 //\r
240 // bit1: Bytes in reserved field may be used by application\r
241 //\r
242 VbeModeInfo->DirectColorModeInfo = BIT1;\r
243\r
244 VbeModeInfo->LfbAddress = (UINT32)FrameBufferBase;\r
245 VbeModeInfo->OffScreenAddress = 0;\r
246 VbeModeInfo->OffScreenSizeKB = 0;\r
247\r
248 VbeModeInfo->BytesPerScanLineLinear = 1024 * 4;\r
249 VbeModeInfo->NumImagesLessOneBanked = 0;\r
250 VbeModeInfo->NumImagesLessOneLinear = 0;\r
251 VbeModeInfo->RedMaskSizeLinear = 8;\r
252 VbeModeInfo->RedMaskPosLinear = 16;\r
253 VbeModeInfo->GreenMaskSizeLinear = 8;\r
254 VbeModeInfo->GreenMaskPosLinear = 8;\r
255 VbeModeInfo->BlueMaskSizeLinear = 8;\r
256 VbeModeInfo->BlueMaskPosLinear = 0;\r
257 VbeModeInfo->ReservedMaskSizeLinear = 8;\r
258 VbeModeInfo->ReservedMaskPosLinear = 24;\r
259 VbeModeInfo->MaxPixelClockHz = 0;\r
260\r
261 ZeroMem (VbeModeInfo->Reserved, sizeof VbeModeInfo->Reserved);\r
262\r
263 //\r
264 // Clear Write Enable (bit1), keep Read Enable (bit0) set\r
265 //\r
266 PciWrite8 (Pam1Address, (Pam1 & ~BIT1) | BIT0);\r
267\r
268 //\r
269 // Second, point the Int10h vector at the shim.\r
270 //\r
ea5396f3
RN
271 Int0x10->Segment = (UINT16) (SegmentC >> 4);\r
272 Int0x10->Offset = (UINT16) ((UINTN) (VbeModeInfo + 1) - SegmentC);\r
90803342
LE
273\r
274 DEBUG ((EFI_D_INFO, "%a: VBE shim installed\n", __FUNCTION__));\r
275}\r