Files
reims-vgpu/firmware/src/GraphicsOutput.c
T
Anees Iqbal a811de7fd0 🐛 Show installers and recovery the EFI framebuffer in BAR 2
macOS installers and recovery images ship no AppleParavirtGPU, so the only
driver that binds the device is IOGraphics' IONDRVFramebuffer, which takes
the EFI boot framebuffer over only when it lies inside one of the device's
memory BARs. The option ROM gave the Graphics Output Protocol its
framebuffer in guest RAM, and the device had one register BAR, so the
installer's WindowServer found no usable display, aborted and started
again and again, the screen frozen on the Apple logo's progress bar.

vendor/qemu's 1a72df98 gives apple-gfx-pci a BAR 2: a 64 MiB window (the
efi-framebuffer-bar property, on by default) holding one alias of the
machine's RAM. After a guest write to 0x1210, QEMU reads the start back
from the library and shows the run of RAM there, up to 64 MiB and never
past what the guest sees of that RAM (so not SMRAM); only the next 0x1210
write and a reset change it.

The option ROM, after it writes the framebuffer's start and length (0x1210,
0x1214), asks PCI I/O for BAR 2's attributes: a memory BAR at least as long
as the buffer, below 4 GiB and with no address translation. It then writes
a pattern and its complement into the buffer's first and last dwords and
reads each back through BAR 2, restoring the dwords. When all four read
back, the GOP's FrameBufferBase is BAR 2's address; otherwise it is the
buffer's own, as before. The buffer stays runtime services data in RAM:
Blt draws into it, the registers name it, and the device presents it, so
the library, its C API, the suite and the pins are unchanged. Those pages
stay the framebuffer's only while the loader keeps boot.efi from moving
runtime data, as OpenCore's AvoidRuntimeDefrag does; the ROM's comment and
architecture.md 11.4 say so.

What the guest sees: one more memory BAR on apple-gfx-pci, which shows the
EFI framebuffer from the ROM's 0x1210 write until the next one or a reset,
and a GOP framebuffer at that BAR's address, which boot.efi hands the
kernel; on a device without BAR 2, the RAM address as before. The ROM's
size is unchanged (5,632 bytes); its bytes change.

docs/qemu.md and docs/architecture.md 11.4 describe BAR 2 and the ROM's
probe, and what the boots measured on the RTX 5080 Laptop GPU host: the
macOS 26.6.2 BaseSystem reaches its Recovery UI (kernel framebuffer at BAR
2, WindowServer started once), and the installed macOS 26 guest reaches
its desktop with BAR 2 on and off, its late draws to the boot framebuffer
landing in RAM.
2026-10-12 00:48:55 +03:00

307 lines
9.5 KiB
C

/** @file
Graphics Output Protocol on a linear 32-bit frame buffer.
**/
#include "ReimsVgpu.h"
//
// A rectangle copy between two surfaces, each described by its row pitch in
// bytes and the pixel coordinates of the rectangle's first corner.
//
typedef struct {
UINT32 SourcePitch;
UINT32 SourceX;
UINT32 SourceY;
UINT32 Width;
UINT32 Height;
UINT32 DestinationPitch;
UINT32 DestinationX;
UINT32 DestinationY;
} REIMS_VGPU_BLT_RECT;
STATIC
VOID
ReimsVgpuSetModeInformation (
OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info
)
{
Info->Version = 0;
Info->PixelFormat = PixelBlueGreenRedReserved8BitPerColor;
Info->PixelInformation.RedMask = 0xFF000000;
Info->PixelInformation.GreenMask = 0x00FF0000;
Info->PixelInformation.BlueMask = 0x0000FF00;
Info->PixelInformation.ReservedMask = 0x000000FF;
Info->PixelsPerScanLine = Info->HorizontalResolution;
}
/**
Publish the frame buffer for the mode just stored in Mode->Info.
**/
STATIC
VOID
ReimsVgpuUpdateMode (
IN REIMS_VGPU_PRIVATE_DATA *Private,
IN EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *Mode
)
{
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
Info = Mode->Info;
ReimsVgpuSetModeInformation (Info);
Mode->FrameBufferBase = Private->FrameBufferBase;
Mode->FrameBufferSize = Info->HorizontalResolution * Info->VerticalResolution * 4;
}
EFI_STATUS
EFIAPI
ReimsVgpuGraphicsOutputQueryMode (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN UINT32 ModeNumber,
OUT UINTN *SizeOfInfo,
OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
)
{
REIMS_VGPU_PRIVATE_DATA *Private;
Private = REIMS_VGPU_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS (This);
if ((Info == NULL) || (SizeOfInfo == NULL) || (ModeNumber >= This->Mode->MaxMode)) {
return EFI_INVALID_PARAMETER;
}
*Info = AllocateRuntimePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));
if (*Info == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
(*Info)->HorizontalResolution = Private->ModeTable[ModeNumber].Width;
(*Info)->VerticalResolution = Private->ModeTable[ModeNumber].Height;
ReimsVgpuSetModeInformation (*Info);
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
ReimsVgpuGraphicsOutputSetMode (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN UINT32 ModeNumber
)
{
REIMS_VGPU_PRIVATE_DATA *Private;
Private = REIMS_VGPU_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS (This);
if (ModeNumber >= This->Mode->MaxMode) {
return EFI_UNSUPPORTED;
}
This->Mode->Mode = ModeNumber;
This->Mode->Info->HorizontalResolution = Private->ModeTable[ModeNumber].Width;
This->Mode->Info->VerticalResolution = Private->ModeTable[ModeNumber].Height;
This->Mode->SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
ReimsVgpuUpdateMode (Private, This->Mode);
ReimsVgpuWriteRegister (Private->PciIo, REIMS_VGPU_REG_EFI_DEPTH, Private->ModeTable[ModeNumber].BitsPerPixel);
ReimsVgpuWriteRegister (
Private->PciIo,
REIMS_VGPU_REG_EFI_MODE,
(Private->ModeTable[ModeNumber].Width & 0xFFFF) | (Private->ModeTable[ModeNumber].Height << 16)
);
return EFI_SUCCESS;
}
/**
Copy a rectangle of 32-bit pixels between two surfaces.
**/
STATIC
VOID
ReimsVgpuCopyRect (
IN UINT8 *Source,
OUT UINT8 *Destination,
IN REIMS_VGPU_BLT_RECT Rect
)
{
UINT32 X;
UINT32 Y;
UINT32 SourceX;
UINT32 SourceY;
UINT32 DestinationX;
UINT32 DestinationY;
UINT8 *SourceRow;
UINT8 *DestinationRow;
for (Y = 0; Y < Rect.Height; Y++) {
SourceY = Rect.SourceY + Y;
DestinationY = Rect.DestinationY + Y;
SourceRow = Source + SourceY * Rect.SourcePitch;
DestinationRow = Destination + DestinationY * Rect.DestinationPitch;
for (X = 0; X < Rect.Width; X++) {
SourceX = Rect.SourceX + X;
DestinationX = Rect.DestinationX + X;
*(UINT32 *)(DestinationRow + DestinationX * sizeof (UINT32)) =
*(UINT32 *)(SourceRow + SourceX * sizeof (UINT32));
}
}
}
/**
Copy a rectangle within one surface. Rows and columns are walked backward
when the destination lies after the source.
**/
STATIC
VOID
ReimsVgpuMoveRect (
IN OUT UINT8 *Buffer,
IN REIMS_VGPU_BLT_RECT Rect
)
{
UINT32 StartDestinationX;
UINT32 StartSourceX;
UINT32 StartSourceY;
UINT32 StartDestinationY;
UINT32 X;
UINT32 Y;
UINT32 OffsetX;
UINT32 OffsetY;
UINT32 SourceX;
UINT32 SourceY;
UINT32 DestinationX;
UINT32 DestinationY;
UINT32 *SourceRow;
UINT32 *DestinationRow;
if (Rect.SourceX < Rect.DestinationX) {
StartSourceX = Rect.SourceX + Rect.Width;
StartDestinationX = StartSourceX + Rect.Width;
} else {
StartSourceX = Rect.SourceX;
StartDestinationX = Rect.DestinationX;
}
if (Rect.SourceY < Rect.DestinationY) {
StartSourceY = Rect.SourceY + Rect.Height;
StartDestinationY = StartSourceY + Rect.Height;
} else {
StartSourceY = Rect.SourceY;
StartDestinationY = Rect.DestinationY;
}
for (Y = 0; Y < Rect.Height; Y++) {
OffsetY = (Rect.SourceY < Rect.DestinationY) ? -Y : Y;
SourceY = StartSourceY + OffsetY;
DestinationY = StartDestinationY + OffsetY;
SourceRow = (UINT32 *)((UINTN)Buffer + SourceY * Rect.SourcePitch);
DestinationRow = (UINT32 *)((UINTN)Buffer + DestinationY * Rect.DestinationPitch);
for (X = 0; X < Rect.Width; X++) {
OffsetX = (Rect.SourceX < Rect.DestinationX) ? -X : X;
SourceX = StartSourceX + OffsetX;
DestinationX = StartDestinationX + OffsetX;
DestinationRow[DestinationX] = SourceRow[SourceX];
}
}
}
EFI_STATUS
EFIAPI
ReimsVgpuGraphicsOutputBlt (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
IN UINTN SourceX,
IN UINTN SourceY,
IN UINTN DestinationX,
IN UINTN DestinationY,
IN UINTN Width,
IN UINTN Height,
IN UINTN Delta OPTIONAL
)
{
REIMS_VGPU_PRIVATE_DATA *Private;
REIMS_VGPU_BLT_RECT Rect;
UINT32 X;
UINT32 Y;
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Row;
Private = REIMS_VGPU_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS (This);
switch (BltOperation) {
case EfiBltVideoFill:
Y = MAX_UINT32;
while (++Y < Height) {
Row = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)((UINT8 *)Private->FrameBuffer +
(UINT32)(DestinationY + Y) * This->Mode->Info->HorizontalResolution * 4);
for (X = 0; X < Width; X++) {
Row[(UINT32)(DestinationX + X)] = *BltBuffer;
}
}
break;
case EfiBltVideoToBltBuffer:
Rect.SourcePitch = This->Mode->Info->HorizontalResolution * 4;
Rect.SourceX = (UINT32)SourceX;
Rect.SourceY = (UINT32)SourceY;
Rect.Width = (UINT32)Width;
Rect.Height = (UINT32)Height;
Rect.DestinationPitch = (UINT32)((Delta != 0) ? Delta : Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
Rect.DestinationX = (UINT32)DestinationX;
Rect.DestinationY = (UINT32)DestinationY;
ReimsVgpuCopyRect (Private->FrameBuffer, (UINT8 *)BltBuffer, Rect);
break;
case EfiBltBufferToVideo:
Rect.SourcePitch = (UINT32)((Delta != 0) ? Delta : Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
Rect.SourceX = (UINT32)SourceX;
Rect.SourceY = (UINT32)SourceY;
Rect.Width = (UINT32)Width;
Rect.Height = (UINT32)Height;
Rect.DestinationPitch = This->Mode->Info->HorizontalResolution * 4;
Rect.DestinationX = (UINT32)DestinationX;
Rect.DestinationY = (UINT32)DestinationY;
ReimsVgpuCopyRect ((UINT8 *)BltBuffer, Private->FrameBuffer, Rect);
break;
case EfiBltVideoToVideo:
Rect.SourcePitch = This->Mode->Info->HorizontalResolution * 4;
Rect.SourceX = (UINT32)SourceX;
Rect.SourceY = (UINT32)SourceY;
Rect.Width = (UINT32)Width;
Rect.Height = (UINT32)Height;
Rect.DestinationPitch = This->Mode->Info->HorizontalResolution * 4;
Rect.DestinationX = (UINT32)DestinationX;
Rect.DestinationY = (UINT32)DestinationY;
ReimsVgpuMoveRect (Private->FrameBuffer, Rect);
break;
default:
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
VOID
ReimsVgpuGraphicsOutputConstructor (
IN REIMS_VGPU_PRIVATE_DATA *Private
)
{
EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
GraphicsOutput = &Private->GraphicsOutput;
GraphicsOutput->QueryMode = ReimsVgpuGraphicsOutputQueryMode;
GraphicsOutput->SetMode = ReimsVgpuGraphicsOutputSetMode;
GraphicsOutput->Blt = ReimsVgpuGraphicsOutputBlt;
GraphicsOutput->Mode = &Private->GraphicsOutputMode;
GraphicsOutput->Mode->MaxMode = Private->MaxMode;
GraphicsOutput->Mode->Mode = 0;
GraphicsOutput->Mode->Info = &Private->ModeInformation;
GraphicsOutput->Mode->SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
ReimsVgpuGraphicsOutputSetMode (GraphicsOutput, 0);
}