VbeShim.c 9.7 KB

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