diff --git a/Core/Driver/Driver.cpp b/Core/Driver/Driver.cpp index 9015c29..8ff440b 100644 --- a/Core/Driver/Driver.cpp +++ b/Core/Driver/Driver.cpp @@ -86,6 +86,7 @@ namespace Driver Fex *fex = (Fex *)KernelAllocator.RequestPages(TO_PAGES(Size)); memcpy(fex, (void *)DriverAddress, Size); FexExtended *fexExtended = (FexExtended *)((uint64_t)fex + EXTENDED_SECTION_ADDRESS); + debug("Driver allocated at %#lx-%#lx", fex, (uint64_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", @@ -102,6 +103,32 @@ namespace Driver KernelCallback *KCallback = (KernelCallback *)KernelAllocator.RequestPages(TO_PAGES(sizeof(KernelCallback))); + debug("Type: %d; IOBase: %#x; MemoryBase: %#x", + ((PCI::PCIHeader0 *)PCIDevice)->BAR0 & 1, + ((PCI::PCIHeader0 *)PCIDevice)->BAR1 & (~3), + ((PCI::PCIHeader0 *)PCIDevice)->BAR0 & (~15)); + + if ((((PCI::PCIHeader0 *)PCIDevice)->BAR0 & 1) != 0) + if (!Memory::Virtual().Check((void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR1 & (~3)))) + { + debug("IO base (BAR1 & ~3) is not mapped"); + Memory::Virtual().Map((void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR1 & (~3)), (void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR1 & (~3)), Memory::PTFlag::RW); + } + + if ((((PCI::PCIHeader0 *)PCIDevice)->BAR0 & 1) == 0) + if (!Memory::Virtual().Check((void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR0 & (~15)))) + { + debug("Memory base (BAR0 & ~15) is not mapped"); + Memory::Virtual().Map((void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR0 & (~15)), (void *)(uint64_t)(((PCI::PCIHeader0 *)PCIDevice)->BAR0 & (~15)), Memory::PTFlag::RW); + + uint64_t original = ((PCI::PCIHeader0 *)PCIDevice)->BAR0; + ((PCI::PCIHeader0 *)PCIDevice)->BAR0 = 0xFFFFFFFF; + uint64_t size = ((PCI::PCIHeader0 *)PCIDevice)->BAR0 & 0xFFFFFFF0; + ((PCI::PCIHeader0 *)PCIDevice)->BAR0 = original; + debug("Size: %#lx (%ld pages)", size, TO_PAGES(size)); + fixme("TODO: [BUG] Mapping is broken!!!!!!"); + } + switch (fexExtended->Driver.Type) { case FexDriverType::FexDriverType_Generic: @@ -122,8 +149,8 @@ namespace Driver KCallback->RawPtr = PCIDevice; KCallback->Reason = CallbackReason::ConfigurationReason; - int callbackret = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); - if (callbackret == DriverReturnCode::NOT_IMPLEMENTED) + int CallbackRet = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); + if (CallbackRet == DriverReturnCode::NOT_IMPLEMENTED) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); @@ -131,54 +158,54 @@ namespace Driver error("Driver %s does not implement the configuration callback", fexExtended->Driver.Name); continue; } - else if (callbackret == DriverReturnCode::OK) + else if (CallbackRet == DriverReturnCode::OK) trace("Device found for driver: %s", fexExtended->Driver.Name); else { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); delete InterruptHook; - error("Driver %s returned error %d", fexExtended->Driver.Name, callbackret); + error("Driver %s returned error %d", fexExtended->Driver.Name, CallbackRet); continue; } memset(KCallback, 0, sizeof(KernelCallback)); KCallback->Reason = CallbackReason::InterruptReason; - DriverFile *drvfile = new DriverFile; - drvfile->DriverUID = KAPI.Info.DriverUID; - drvfile->Address = (void *)fex; - drvfile->InterruptHook[0] = InterruptHook; - Drivers.push_back(drvfile); + DriverFile *DrvFile = new DriverFile; + DrvFile->DriverUID = KAPI.Info.DriverUID; + DrvFile->Address = (void *)fex; + DrvFile->InterruptHook[0] = InterruptHook; + Drivers.push_back(DrvFile); break; } case FexDriverType::FexDriverType_Storage: { KCallback->RawPtr = PCIDevice; KCallback->Reason = CallbackReason::ConfigurationReason; - int callbackret = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); - if (callbackret == DriverReturnCode::NOT_IMPLEMENTED) + int CallbackRet = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); + if (CallbackRet == DriverReturnCode::NOT_IMPLEMENTED) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); error("Driver %s does not implement the configuration callback", fexExtended->Driver.Name); continue; } - else if (callbackret == DriverReturnCode::OK) + else if (CallbackRet == DriverReturnCode::OK) trace("Device found for driver: %s", fexExtended->Driver.Name); else { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - error("Driver %s returned error %d", fexExtended->Driver.Name, callbackret); + error("Driver %s returned error %d", fexExtended->Driver.Name, CallbackRet); continue; } - DriverFile *drvfile = new DriverFile; - drvfile->DriverUID = KAPI.Info.DriverUID; - drvfile->Address = (void *)fex; - drvfile->InterruptHook[0] = nullptr; - Drivers.push_back(drvfile); + DriverFile *DrvFile = new DriverFile; + DrvFile->DriverUID = KAPI.Info.DriverUID; + DrvFile->Address = (void *)fex; + DrvFile->InterruptHook[0] = nullptr; + Drivers.push_back(DrvFile); break; } case FexDriverType::FexDriverType_FileSystem: @@ -210,6 +237,7 @@ namespace Driver Fex *fex = (Fex *)KernelAllocator.RequestPages(TO_PAGES(Size)); memcpy(fex, (void *)DriverAddress, Size); FexExtended *fexExtended = (FexExtended *)((uint64_t)fex + EXTENDED_SECTION_ADDRESS); + debug("Driver allocated at %#lx-%#lx", fex, (uint64_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", @@ -260,27 +288,27 @@ namespace Driver KCallback->RawPtr = nullptr; KCallback->Reason = CallbackReason::ConfigurationReason; - int callbackret = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); - if (callbackret == DriverReturnCode::NOT_IMPLEMENTED) + int CallbackRet = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); + if (CallbackRet == DriverReturnCode::NOT_IMPLEMENTED) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); error("Driver %s does not implement the configuration callback", fexExtended->Driver.Name); break; } - else if (callbackret != DriverReturnCode::OK) + else if (CallbackRet != DriverReturnCode::OK) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - error("Driver %s returned error %d", fexExtended->Driver.Name, callbackret); + error("Driver %s returned error %d", fexExtended->Driver.Name, CallbackRet); break; } KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - // DriverFile *drvfile = new DriverFile; - // Drivers.push_back(drvfile); + // DriverFile *DrvFile = new DriverFile; + // Drivers.push_back(DrvFile); break; } case FexDriverType::FexDriverType_FileSystem: @@ -308,30 +336,30 @@ namespace Driver KCallback->RawPtr = nullptr; KCallback->Reason = CallbackReason::ConfigurationReason; - int callbackret = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); - if (callbackret == DriverReturnCode::NOT_IMPLEMENTED) + int CallbackRet = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); + if (CallbackRet == DriverReturnCode::NOT_IMPLEMENTED) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); error("Driver %s does not implement the configuration callback", fexExtended->Driver.Name); break; } - else if (callbackret != DriverReturnCode::OK) + else if (CallbackRet != DriverReturnCode::OK) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - error("Driver %s returned error %d", fexExtended->Driver.Name, callbackret); + error("Driver %s returned error %d", fexExtended->Driver.Name, CallbackRet); break; } memset(KCallback, 0, sizeof(KernelCallback)); KCallback->Reason = CallbackReason::InterruptReason; - DriverFile *drvfile = new DriverFile; - drvfile->DriverUID = KAPI.Info.DriverUID; - drvfile->Address = (void *)fex; - drvfile->InterruptHook[0] = InterruptHook; - Drivers.push_back(drvfile); + DriverFile *DrvFile = new DriverFile; + DrvFile->DriverUID = KAPI.Info.DriverUID; + DrvFile->Address = (void *)fex; + DrvFile->InterruptHook[0] = InterruptHook; + Drivers.push_back(DrvFile); break; } case FexDriverType::FexDriverType_Audio: @@ -355,6 +383,7 @@ namespace Driver Fex *fex = (Fex *)KernelAllocator.RequestPages(TO_PAGES(Size)); memcpy(fex, (void *)DriverAddress, Size); FexExtended *fexExtended = (FexExtended *)((uint64_t)fex + EXTENDED_SECTION_ADDRESS); + debug("Driver allocated at %#lx-%#lx", fex, (uint64_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", @@ -379,30 +408,30 @@ namespace Driver KCallback->RawPtr = nullptr; break; KCallback->Reason = CallbackReason::ConfigurationReason; - int callbackret = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); - if (callbackret == DriverReturnCode::NOT_IMPLEMENTED) + int CallbackRet = ((int (*)(KernelCallback *))((uint64_t)fexExtended->Driver.Callback + (uint64_t)fex))(KCallback); + if (CallbackRet == DriverReturnCode::NOT_IMPLEMENTED) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); error("Driver %s does not implement the configuration callback", fexExtended->Driver.Name); break; } - else if (callbackret != DriverReturnCode::OK) + else if (CallbackRet != DriverReturnCode::OK) { KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - error("Driver %s returned error %d", fexExtended->Driver.Name, callbackret); + error("Driver %s returned error %d", fexExtended->Driver.Name, CallbackRet); break; } KernelAllocator.FreePages(fex, TO_PAGES(Size)); KernelAllocator.FreePages(KCallback, TO_PAGES(sizeof(KernelCallback))); - DriverFile *drvfile = new DriverFile; - drvfile->DriverUID = KAPI.Info.DriverUID; - drvfile->Address = (void *)fex; - drvfile->InterruptHook[0] = nullptr; - Drivers.push_back(drvfile); + DriverFile *DrvFile = new DriverFile; + DrvFile->DriverUID = KAPI.Info.DriverUID; + DrvFile->Address = (void *)fex; + DrvFile->InterruptHook[0] = nullptr; + Drivers.push_back(DrvFile); break; } default: