mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-17 02:01:44 +00:00
Move drivers to kernel
This commit is contained in:
@@ -31,171 +31,114 @@
|
||||
|
||||
namespace Driver
|
||||
{
|
||||
void Driver::MapPCIAddresses(PCI::PCIDeviceHeader *PCIDevice)
|
||||
{
|
||||
debug("Header Type: %d", PCIDevice->HeaderType);
|
||||
switch (PCIDevice->HeaderType)
|
||||
{
|
||||
case 0: // PCI Header 0
|
||||
{
|
||||
uint32_t BAR[6] = {0};
|
||||
size_t BARsSize[6] = {0};
|
||||
DriverCode Driver::DriverLoadBindPCI(uintptr_t DriverAddress, size_t Size, bool IsBuiltIn)
|
||||
{
|
||||
FexExtended *DrvExtHdr = (FexExtended *)(DriverAddress + EXTENDED_SECTION_ADDRESS);
|
||||
if (IsBuiltIn)
|
||||
DrvExtHdr = (FexExtended *)(((BuiltInDriverInfo *)DriverAddress)->ExtendedHeader);
|
||||
|
||||
BAR[0] = ((PCI::PCIHeader0 *)PCIDevice)->BAR0;
|
||||
BAR[1] = ((PCI::PCIHeader0 *)PCIDevice)->BAR1;
|
||||
BAR[2] = ((PCI::PCIHeader0 *)PCIDevice)->BAR2;
|
||||
BAR[3] = ((PCI::PCIHeader0 *)PCIDevice)->BAR3;
|
||||
BAR[4] = ((PCI::PCIHeader0 *)PCIDevice)->BAR4;
|
||||
BAR[5] = ((PCI::PCIHeader0 *)PCIDevice)->BAR5;
|
||||
uint16_t SizeOfVendorID = sizeof(DrvExtHdr->Driver.Bind.PCI.VendorID) /
|
||||
sizeof(DrvExtHdr->Driver.Bind.PCI.VendorID[0]);
|
||||
uint16_t SizeOfDeviceID = sizeof(DrvExtHdr->Driver.Bind.PCI.DeviceID) /
|
||||
sizeof(DrvExtHdr->Driver.Bind.PCI.DeviceID[0]);
|
||||
|
||||
#ifdef DEBUG
|
||||
uintptr_t BAR_Type = BAR[0] & 1;
|
||||
uintptr_t BAR_IOBase = BAR[1] & (~3);
|
||||
uintptr_t BAR_MemoryBase = BAR[0] & (~15);
|
||||
for (uint16_t vID = 0; vID < SizeOfVendorID; vID++)
|
||||
{
|
||||
for (uint16_t dID = 0; dID < SizeOfDeviceID; dID++)
|
||||
{
|
||||
if (DrvExtHdr->Driver.Bind.PCI.VendorID[vID] == 0 ||
|
||||
DrvExtHdr->Driver.Bind.PCI.DeviceID[dID] == 0)
|
||||
continue;
|
||||
|
||||
debug("Type: %d; IOBase: %#lx; MemoryBase: %#lx", BAR_Type, BAR_IOBase, BAR_MemoryBase);
|
||||
#endif
|
||||
std::vector<PCI::PCIDeviceHeader *> devices =
|
||||
PCIManager->FindPCIDevice(DrvExtHdr->Driver.Bind.PCI.VendorID[vID],
|
||||
DrvExtHdr->Driver.Bind.PCI.DeviceID[dID]);
|
||||
if (devices.size() == 0)
|
||||
continue;
|
||||
|
||||
/* BARs Size */
|
||||
for (short i = 0; i < 6; i++)
|
||||
{
|
||||
if (BAR[i] == 0)
|
||||
continue;
|
||||
foreach (auto PCIDevice in devices)
|
||||
{
|
||||
debug("[%ld] VendorID: %#x; DeviceID: %#x",
|
||||
devices.size(), PCIDevice->VendorID, PCIDevice->DeviceID);
|
||||
|
||||
if ((BAR[i] & 1) == 0) // Memory Base
|
||||
{
|
||||
((PCI::PCIHeader0 *)PCIDevice)->BAR0 = 0xFFFFFFFF;
|
||||
size_t size = ((PCI::PCIHeader0 *)PCIDevice)->BAR0;
|
||||
((PCI::PCIHeader0 *)PCIDevice)->BAR0 = BAR[i];
|
||||
BARsSize[i] = size & (~15);
|
||||
BARsSize[i] = ~BARsSize[i] + 1;
|
||||
BARsSize[i] = BARsSize[i] & 0xFFFFFFFF;
|
||||
debug("BAR%d %#lx size: %d", i, BAR[i], BARsSize[i]);
|
||||
}
|
||||
else if ((BAR[i] & 1) == 1) // I/O Base
|
||||
{
|
||||
((PCI::PCIHeader0 *)PCIDevice)->BAR1 = 0xFFFFFFFF;
|
||||
size_t size = ((PCI::PCIHeader0 *)PCIDevice)->BAR1;
|
||||
((PCI::PCIHeader0 *)PCIDevice)->BAR1 = BAR[i];
|
||||
BARsSize[i] = size & (~3);
|
||||
BARsSize[i] = ~BARsSize[i] + 1;
|
||||
BARsSize[i] = BARsSize[i] & 0xFFFF;
|
||||
debug("BAR%d %#lx size: %d", i, BAR[i], BARsSize[i]);
|
||||
}
|
||||
}
|
||||
Memory::MemMgr *mem = new Memory::MemMgr(nullptr, TaskManager->GetCurrentProcess()->memDirectory);
|
||||
|
||||
/* Mapping the BARs */
|
||||
for (short i = 0; i < 6; i++)
|
||||
{
|
||||
if (BAR[i] == 0)
|
||||
continue;
|
||||
BuiltInDriverInfo *bidi = (BuiltInDriverInfo *)DriverAddress;
|
||||
Fex *fex = nullptr;
|
||||
if (!IsBuiltIn)
|
||||
{
|
||||
fex = (Fex *)mem->RequestPages(TO_PAGES(Size + 1));
|
||||
memcpy(fex, (void *)DriverAddress, Size);
|
||||
debug("Driver allocated at %#lx-%#lx", fex, (uintptr_t)fex + Size);
|
||||
}
|
||||
else
|
||||
fex = (Fex *)bidi->EntryPoint;
|
||||
DriverCode ret = CallDriverEntryPoint(fex, IsBuiltIn);
|
||||
if (ret != DriverCode::OK)
|
||||
{
|
||||
delete mem;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((BAR[i] & 1) == 0) // Memory Base
|
||||
{
|
||||
uintptr_t BARBase = BAR[i] & (~15);
|
||||
size_t BARSize = BARsSize[i];
|
||||
if (IsBuiltIn)
|
||||
fex = 0x0; /* Addresses are absolute if built-in. */
|
||||
|
||||
debug("Mapping BAR%d %#lx-%#lx", i, BARBase, BARBase + BARSize);
|
||||
Memory::Virtual().Map((void *)BARBase, (void *)BARBase, BARSize, Memory::PTFlag::RW | Memory::PTFlag::PWT);
|
||||
}
|
||||
else if ((BAR[i] & 1) == 1) // I/O Base
|
||||
{
|
||||
uintptr_t BARBase = BAR[i] & (~3);
|
||||
size_t BARSize = BARsSize[i];
|
||||
FexExtended *fexE = IsBuiltIn ? (FexExtended *)bidi->ExtendedHeader : (FexExtended *)((uintptr_t)fex + EXTENDED_SECTION_ADDRESS);
|
||||
debug("Starting driver %s", fexE->Driver.Name);
|
||||
|
||||
debug("Mapping BAR%d %#x-%#x", i, BARBase, BARBase + BARSize);
|
||||
Memory::Virtual().Map((void *)BARBase, (void *)BARBase, BARSize, Memory::PTFlag::RW | Memory::PTFlag::PWT);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: // PCI Header 1 (PCI-to-PCI Bridge)
|
||||
{
|
||||
fixme("PCI Header 1 (PCI-to-PCI Bridge) not implemented yet");
|
||||
break;
|
||||
}
|
||||
case 2: // PCI Header 2 (PCI-to-CardBus Bridge)
|
||||
{
|
||||
fixme("PCI Header 2 (PCI-to-CardBus Bridge) not implemented yet");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
error("Unknown header type %d", PCIDevice->HeaderType);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
PCIManager->MapPCIAddresses(PCIDevice);
|
||||
|
||||
DriverCode Driver::DriverLoadBindPCI(void *DrvExtHdr, uintptr_t DriverAddress, size_t Size, bool IsElf)
|
||||
{
|
||||
UNUSED(IsElf);
|
||||
for (unsigned long Vidx = 0; Vidx < sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID) / sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[0]); Vidx++)
|
||||
{
|
||||
for (unsigned long Didx = 0; Didx < sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID) / sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[0]); Didx++)
|
||||
{
|
||||
if (Vidx >= sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID) && Didx >= sizeof(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID))
|
||||
break;
|
||||
switch (fexE->Driver.Type)
|
||||
{
|
||||
case FexDriverType::FexDriverType_Generic:
|
||||
case FexDriverType::FexDriverType_Display:
|
||||
case FexDriverType::FexDriverType_Network:
|
||||
case FexDriverType::FexDriverType_Storage:
|
||||
case FexDriverType::FexDriverType_FileSystem:
|
||||
case FexDriverType::FexDriverType_Input:
|
||||
case FexDriverType::FexDriverType_Audio:
|
||||
{
|
||||
FexExtended *DriverExtendedHeader = (FexExtended *)mem->RequestPages(TO_PAGES(sizeof(FexExtended) + 1));
|
||||
memcpy(DriverExtendedHeader, fexE, sizeof(FexExtended));
|
||||
|
||||
if (((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx] == 0 || ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx] == 0)
|
||||
continue;
|
||||
DriverFile DrvFile = {
|
||||
.Enabled = true,
|
||||
.BuiltIn = IsBuiltIn,
|
||||
.DriverUID = this->DriverUIDs - 1,
|
||||
.Address = (void *)fex,
|
||||
.ExtendedHeaderAddress = (void *)DriverExtendedHeader,
|
||||
.InterruptCallback = (void *)((uintptr_t)fex + (uintptr_t)fexE->Driver.InterruptCallback),
|
||||
.MemTrk = mem,
|
||||
};
|
||||
|
||||
std::vector<PCI::PCIDeviceHeader *> devices = PCIManager->FindPCIDevice(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx], ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx]);
|
||||
if (devices.size() == 0)
|
||||
continue;
|
||||
if (fexE->Driver.InterruptCallback)
|
||||
DrvFile.InterruptHook[0] = new DriverInterruptHook(((int)((PCI::PCIHeader0 *)PCIDevice)->InterruptLine), DrvFile);
|
||||
|
||||
foreach (auto PCIDevice in devices)
|
||||
{
|
||||
debug("[%ld] VendorID: %#x; DeviceID: %#x", devices.size(), PCIDevice->VendorID, PCIDevice->DeviceID);
|
||||
Memory::MemMgr *mem = new Memory::MemMgr(nullptr, TaskManager->GetCurrentProcess()->memDirectory);
|
||||
Fex *fex = (Fex *)mem->RequestPages(TO_PAGES(Size + 1));
|
||||
memcpy(fex, (void *)DriverAddress, Size);
|
||||
FexExtended *fexExtended = (FexExtended *)((uintptr_t)fex + EXTENDED_SECTION_ADDRESS);
|
||||
debug("Driver allocated at %#lx-%#lx", fex, (uintptr_t)fex + Size);
|
||||
#ifdef DEBUG
|
||||
uint8_t *result = md5File((uint8_t *)fex, Size);
|
||||
debug("MD5: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
|
||||
result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
|
||||
result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]);
|
||||
kfree(result);
|
||||
#endif
|
||||
KernelAPI *KAPI = (KernelAPI *)mem->RequestPages(TO_PAGES(sizeof(KernelAPI) + 1));
|
||||
KernelCallback KCallback{};
|
||||
KCallback.RawPtr = PCIDevice;
|
||||
KCallback.Reason = CallbackReason::ConfigurationReason;
|
||||
DriverCode CallbackRet = ((DriverCode(*)(KernelCallback *))((uintptr_t)fexE->Driver.Callback + (uintptr_t)fex))(&KCallback);
|
||||
|
||||
if (CallDriverEntryPoint(fex, KAPI) != DriverCode::OK)
|
||||
{
|
||||
delete mem, mem = nullptr;
|
||||
return DriverCode::DRIVER_RETURNED_ERROR;
|
||||
}
|
||||
debug("Starting driver %s", fexExtended->Driver.Name);
|
||||
if (CallbackRet != DriverCode::OK)
|
||||
{
|
||||
error("Driver %s returned error %d", fexE->Driver.Name, CallbackRet);
|
||||
delete mem;
|
||||
return CallbackRet;
|
||||
}
|
||||
|
||||
MapPCIAddresses(PCIDevice);
|
||||
|
||||
switch (fexExtended->Driver.Type)
|
||||
{
|
||||
case FexDriverType::FexDriverType_Generic:
|
||||
return BindPCIGeneric(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_Display:
|
||||
return BindPCIDisplay(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_Network:
|
||||
return BindPCINetwork(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_Storage:
|
||||
return BindPCIStorage(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_FileSystem:
|
||||
return BindPCIFileSystem(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_Input:
|
||||
return BindPCIInput(mem, fex, PCIDevice);
|
||||
case FexDriverType::FexDriverType_Audio:
|
||||
return BindPCIAudio(mem, fex, PCIDevice);
|
||||
default:
|
||||
{
|
||||
warn("Unknown driver type: %d", fexExtended->Driver.Type);
|
||||
delete mem, mem = nullptr;
|
||||
return DriverCode::UNKNOWN_DRIVER_TYPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return DriverCode::PCI_DEVICE_NOT_FOUND;
|
||||
}
|
||||
Drivers.push_back(DrvFile);
|
||||
return DriverCode::OK;
|
||||
}
|
||||
default:
|
||||
{
|
||||
warn("Unknown driver type: %d", fexE->Driver.Type);
|
||||
delete mem;
|
||||
return DriverCode::UNKNOWN_DRIVER_TYPE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return DriverCode::PCI_DEVICE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user