using XFESystem.Boot;
namespace XFESystem.NativeKernel;
// Read the immutable, pointer-free PCI resource snapshot published by our UEFI
// shim. UEFI Boot/Runtime Services are never called after ExitBootServices.
public static unsafe class NativeGpuFirmware
{
static FirmwareGpuHandoff* _report;
static bool _initialized;
static uint _locateState, _matchState, _bar0State;
static ulong _tablePhysical;
// Additive, value-only diagnostics. No firmware pointers cross the ABI.
// Locate: 1 boot context, 2 table mapping, 3 signature, 4 header/CRC,
// 5 array shape, 6 array mapping, 7 missing GUID, 8 duplicate GUID,
// 9 report mapping, 10 report schema/CRC, 11 accepted.
public static ulong Diagnostic(uint field)
{
if (field == 0) return 1;
if (field == 1) return _locateState;
if (field == 2) return _report == null ? 0UL : _report->Count;
if (field == 3) return _tablePhysical;
if (field == 4) return _matchState;
if (field == 5) return _bar0State;
return 0;
}
static byte* PhysicalPointer(NativeBootInfo* boot, ulong physical, ulong bytes)
{
if (boot == null || boot->MemoryEntries == null || boot->MemoryEntryCount == 0 || boot->MemoryEntryCount > 4096 ||
physical == 0 || bytes == 0 || physical >= (1UL << 52) || bytes > (1UL << 52) - physical ||
boot->Hhdm == 0 || physical > ~0UL - boot->Hhdm || bytes - 1 > ~0UL - (boot->Hhdm + physical)) return null;
// EfiRuntimeServicesData and the EFI table are Reserved(Mapped) under
// Limine base6. Never dereference an address merely because it is HHDM.
bool covered = false;
for (ulong i = 0; i < boot->MemoryEntryCount; i++)
{
MemoryMapEntry* entry = boot->MemoryEntries[i];
if (entry == null || entry->Length > ~0UL - entry->Base) return null;
if (entry->Length != 0 && physical < entry->Base + entry->Length && entry->Base < physical + bytes && entry->Kind != 8)
return null;
if (entry != null && entry->Kind == 8 && physical >= entry->Base &&
physical - entry->Base < entry->Length && bytes <= entry->Length - (physical - entry->Base))
covered = true;
}
return covered ? (byte*)(boot->Hhdm + physical) : null;
}
static uint HeaderCrc(byte* header, uint bytes)
{
uint crc = 0xffffffff;
for (uint i = 0; i < bytes; i++)
{
crc ^= i >= 16 && i < 20 ? (byte)0 : header[i];
for (uint bit = 0; bit < 8; bit++) crc = (crc >> 1) ^ ((crc & 1) != 0 ? 0xedb88320u : 0);
}
return ~crc;
}
public static void Initialize(NativeBootInfo* boot)
{
if (_initialized) return;
_initialized = true;
_report = Locate(boot);
Serial.Write("XFE_GPU_FIRMWARE_HANDOFF|Valid|"); Serial.Number(_report == null ? 0UL : 1UL);
Serial.Write("|Count|"); Serial.Number(_report == null ? 0UL : _report->Count);
Serial.Line("|ResourceOnly|1|Cuda|0|GpuRasterization|0");
}
static FirmwareGpuHandoff* Locate(NativeBootInfo* boot)
{
_locateState = 1;
if (boot == null || boot->MemoryEntries == null || boot->MemoryEntryCount == 0 ||
boot->MemoryEntryCount > 4096 || boot->EfiSystemTable < boot->Hhdm) return null;
ulong physical = boot->EfiSystemTable - boot->Hhdm;
_tablePhysical = physical;
_locateState = 2;
byte* table = PhysicalPointer(boot, physical, 120);
if (table == null) return null;
_locateState = 3;
if (*(ulong*)table != 0x5453595320494249UL || *(uint*)(table + 20) != 0) return null;
uint headerBytes = *(uint*)(table + 12);
_locateState = 4;
if (headerBytes < 120 || headerBytes > 4096 || PhysicalPointer(boot, physical, headerBytes) == null ||
*(uint*)(table + 16) != HeaderCrc(table, headerBytes)) return null;
ulong count = *(ulong*)(table + 104), arrayPhysical = *(ulong*)(table + 112);
_locateState = 5;
if (count == 0 || count > 4096) return null;
_locateState = 6;
byte* entries = PhysicalPointer(boot, arrayPhysical, count * 24);
if (entries == null) return null;
_locateState = 7;
FirmwareGpuHandoff* selected = null;
for (ulong i = 0; i < count; i++)
{
byte* entry = entries + i * 24;
if (*(ulong*)entry != FirmwareGpuHandoff.GuidLow || *(ulong*)(entry + 8) != FirmwareGpuHandoff.GuidHigh) continue;
_locateState = 8;
if (selected != null) return null;
_locateState = 9;
FirmwareGpuHandoff* report = (FirmwareGpuHandoff*)PhysicalPointer(boot, *(ulong*)(entry + 16), FirmwareGpuHandoff.TotalBytes);
if (report == null) return null;
_locateState = 10;
if (!FirmwareGpuResources.Validate(report, FirmwareGpuHandoff.TotalBytes) || report->Flags != 0) return null;
selected = report;
}
_locateState = selected == null ? 7u : 11u;
return selected;
}
public static void Apply(NativeBootInfo* boot, NativeGpuDeviceDiscovery* record)
{
Initialize(boot);
bool diagnose = record != null && record->VendorId == 0x10de && record->DeviceId == 0x2b85;
if (diagnose) { _matchState = 1; _bar0State = 0; }
if (_report == null || record == null) return;
if (diagnose) _matchState = 2;
FirmwareGpuDevice* selected = null;
for (uint i = 0; i < _report->Count; i++)
{
FirmwareGpuDevice* item = (FirmwareGpuDevice*)(_report->Devices + i * FirmwareGpuHandoff.DeviceBytes);
if (item->Segment != record->Segment || item->Bus != record->Bus || item->Device != record->Device ||
item->Function != record->Function) continue;
if (selected != null) { if (diagnose) _matchState = 4; return; }
if (item->VendorId != record->VendorId || item->DeviceId != record->DeviceId ||
item->ClassCode != record->ClassCode || (item->Command & 3) != (record->Command & 3))
{ if (diagnose) _matchState = 3; return; }
selected = item;
}
if (selected == null) return;
if (diagnose) _matchState = 5;
for (uint i = 0; i < 6; i++)
{
FirmwareGpuBar* source = (FirmwareGpuBar*)(selected->Bars + i * 24);
if (diagnose && i == 0) _bar0State = source->Flags == 0 ? 1u : 2u;
if (source->Index != i || (source->Flags & 7) != 3 || (source->Flags & ~31u) != 0) continue;
NativeGpuBarDiscovery* target = &record->Bar0 + i;
// Firmware descriptors cannot reinterpret an I/O BAR or the high
// DWORD of a 64-bit BAR. ApplyResourceSize checks the live address.
if (diagnose && i == 0) _bar0State = 3;
if ((target->Flags & 1) == 0 || ((target->Flags & 4) != 0) != ((source->Flags & 8) != 0) ||
((target->Flags & 8) != 0) != ((source->Flags & 16) != 0)) continue;
bool applied = GpuHardwareBridge.ApplyResourceSize(target, source->Address, source->Bytes, 2);
if (diagnose && i == 0) _bar0State = applied ? 5u : 4u;
}
}
}
using XFESystem.Boot;
namespace XFESystem.NativeKernel;
// Read the immutable, pointer-free PCI resource snapshot published by our UEFI
// shim. UEFI Boot/Runtime Services are never called after ExitBootServices.
public static unsafe class NativeGpuFirmware
{
static FirmwareGpuHandoff* _report;
static bool _initialized;
static uint _locateState, _matchState, _bar0State;
static ulong _tablePhysical;
// Additive, value-only diagnostics. No firmware pointers cross the ABI.
// Locate: 1 boot context, 2 table mapping, 3 signature, 4 header/CRC,
// 5 array shape, 6 array mapping, 7 missing GUID, 8 duplicate GUID,
// 9 report mapping, 10 report schema/CRC, 11 accepted.
public static ulong Diagnostic(uint field)
{
if (field == 0) return 1;
if (field == 1) return _locateState;
if (field == 2) return _report == null ? 0UL : _report->Count;
if (field == 3) return _tablePhysical;
if (field == 4) return _matchState;
if (field == 5) return _bar0State;
return 0;
}
static byte* PhysicalPointer(NativeBootInfo* boot, ulong physical, ulong bytes)
{
if (boot == null || boot->MemoryEntries == null || boot->MemoryEntryCount == 0 || boot->MemoryEntryCount > 4096 ||
physical == 0 || bytes == 0 || physical >= (1UL << 52) || bytes > (1UL << 52) - physical ||
boot->Hhdm == 0 || physical > ~0UL - boot->Hhdm || bytes - 1 > ~0UL - (boot->Hhdm + physical)) return null;
// EfiRuntimeServicesData and the EFI table are Reserved(Mapped) under
// Limine base6. Never dereference an address merely because it is HHDM.
bool covered = false;
for (ulong i = 0; i < boot->MemoryEntryCount; i++)
{
MemoryMapEntry* entry = boot->MemoryEntries[i];
if (entry == null || entry->Length > ~0UL - entry->Base) return null;
if (entry->Length != 0 && physical < entry->Base + entry->Length && entry->Base < physical + bytes && entry->Kind != 8)
return null;
if (entry != null && entry->Kind == 8 && physical >= entry->Base &&
physical - entry->Base < entry->Length && bytes <= entry->Length - (physical - entry->Base))
covered = true;
}
return covered ? (byte*)(boot->Hhdm + physical) : null;
}
static uint HeaderCrc(byte* header, uint bytes)
{
uint crc = 0xffffffff;
for (uint i = 0; i < bytes; i++)
{
crc ^= i >= 16 && i < 20 ? (byte)0 : header[i];
for (uint bit = 0; bit < 8; bit++) crc = (crc >> 1) ^ ((crc & 1) != 0 ? 0xedb88320u : 0);
}
return ~crc;
}
public static void Initialize(NativeBootInfo* boot)
{
if (_initialized) return;
_initialized = true;
_report = Locate(boot);
Serial.Write("XFE_GPU_FIRMWARE_HANDOFF|Valid|"); Serial.Number(_report == null ? 0UL : 1UL);
Serial.Write("|Count|"); Serial.Number(_report == null ? 0UL : _report->Count);
Serial.Line("|ResourceOnly|1|Cuda|0|GpuRasterization|0");
}
static FirmwareGpuHandoff* Locate(NativeBootInfo* boot)
{
_locateState = 1;
if (boot == null || boot->MemoryEntries == null || boot->MemoryEntryCount == 0 ||
boot->MemoryEntryCount > 4096 || boot->EfiSystemTable < boot->Hhdm) return null;
ulong physical = boot->EfiSystemTable - boot->Hhdm;
_tablePhysical = physical;
_locateState = 2;
byte* table = PhysicalPointer(boot, physical, 120);
if (table == null) return null;
_locateState = 3;
if (*(ulong*)table != 0x5453595320494249UL || *(uint*)(table + 20) != 0) return null;
uint headerBytes = *(uint*)(table + 12);
_locateState = 4;
if (headerBytes < 120 || headerBytes > 4096 || PhysicalPointer(boot, physical, headerBytes) == null ||
*(uint*)(table + 16) != HeaderCrc(table, headerBytes)) return null;
ulong count = *(ulong*)(table + 104), arrayPhysical = *(ulong*)(table + 112);
_locateState = 5;
if (count == 0 || count > 4096) return null;
_locateState = 6;
byte* entries = PhysicalPointer(boot, arrayPhysical, count * 24);
if (entries == null) return null;
_locateState = 7;
FirmwareGpuHandoff* selected = null;
for (ulong i = 0; i < count; i++)
{
byte* entry = entries + i * 24;
if (*(ulong*)entry != FirmwareGpuHandoff.GuidLow || *(ulong*)(entry + 8) != FirmwareGpuHandoff.GuidHigh) continue;
_locateState = 8;
if (selected != null) return null;
_locateState = 9;
FirmwareGpuHandoff* report = (FirmwareGpuHandoff*)PhysicalPointer(boot, *(ulong*)(entry + 16), FirmwareGpuHandoff.TotalBytes);
if (report == null) return null;
_locateState = 10;
if (!FirmwareGpuResources.Validate(report, FirmwareGpuHandoff.TotalBytes) || report->Flags != 0) return null;
selected = report;
}
_locateState = selected == null ? 7u : 11u;
return selected;
}
public static void Apply(NativeBootInfo* boot, NativeGpuDeviceDiscovery* record)
{
Initialize(boot);
bool diagnose = record != null && record->VendorId == 0x10de && record->DeviceId == 0x2b85;
if (diagnose) { _matchState = 1; _bar0State = 0; }
if (_report == null || record == null) return;
if (diagnose) _matchState = 2;
FirmwareGpuDevice* selected = null;
for (uint i = 0; i < _report->Count; i++)
{
FirmwareGpuDevice* item = (FirmwareGpuDevice*)(_report->Devices + i * FirmwareGpuHandoff.DeviceBytes);
if (item->Segment != record->Segment || item->Bus != record->Bus || item->Device != record->Device ||
item->Function != record->Function) continue;
if (selected != null) { if (diagnose) _matchState = 4; return; }
if (item->VendorId != record->VendorId || item->DeviceId != record->DeviceId ||
item->ClassCode != record->ClassCode || (item->Command & 3) != (record->Command & 3))
{ if (diagnose) _matchState = 3; return; }
selected = item;
}
if (selected == null) return;
if (diagnose) _matchState = 5;
for (uint i = 0; i < 6; i++)
{
FirmwareGpuBar* source = (FirmwareGpuBar*)(selected->Bars + i * 24);
if (diagnose && i == 0) _bar0State = source->Flags == 0 ? 1u : 2u;
if (source->Index != i || (source->Flags & 7) != 3 || (source->Flags & ~31u) != 0) continue;
NativeGpuBarDiscovery* target = &record->Bar0 + i;
// Firmware descriptors cannot reinterpret an I/O BAR or the high
// DWORD of a 64-bit BAR. ApplyResourceSize checks the live address.
if (diagnose && i == 0) _bar0State = 3;
if ((target->Flags & 1) == 0 || ((target->Flags & 4) != 0) != ((source->Flags & 8) != 0) ||
((target->Flags & 8) != 0) != ((source->Flags & 16) != 0)) continue;
bool applied = GpuHardwareBridge.ApplyResourceSize(target, source->Address, source->Bytes, 2);
if (diagnose && i == 0) _bar0State = applied ? 5u : 4u;
}
}
}