Fix driver implementation

This commit is contained in:
EnderIce2
2024-07-07 03:15:17 +03:00
parent f85935f8f3
commit 4ecf37c44e
64 changed files with 2893 additions and 2863 deletions

View File

@ -1,83 +1,22 @@
# Config file
# Config files
include ../../../Makefile.conf
FILENAME = ahci.drv
CC = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)gcc
CPP = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)g++
LD = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)ld
AS = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)as
OBJDUMP = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)objdump
include ../../config.mk
S_SOURCES = $(shell find ./ -type f -name '*.S')
C_SOURCES = $(shell find ./ -type f -name '*.c')
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp')
HEADERS = $(sort $(dir $(wildcard ../../include/*)))
OBJ = $(C_SOURCES:.c=.o) $(CPP_SOURCES:.cpp=.o) $(ASM_SOURCES:.asm=.o) $(S_SOURCES:.S=.o) $(PSF_SOURCES:.psf=.o) $(BMP_SOURCES:.bmp=.o)
OBJ = $(C_SOURCES:.c=.o) $(CPP_SOURCES:.cpp=.o) $(S_SOURCES:.S=.o)
STACK_USAGE_OBJ = $(C_SOURCES:.c=.su) $(CPP_SOURCES:.cpp=.su)
INCLUDE_DIR = ../../include
LIBS := ../../out/dcrt0.o -L../../out -ldriver
LDFLAGS := \
-fPIC -fPIE -pie \
-Wl,--no-dynamic-linker,-ztext,--no-warn-rwx-segment \
-nostdlib -nodefaultlibs -nolibc \
-zmax-page-size=0x1000 \
-Wl,-Map file.map -shared -fvisibility=hidden
WARNCFLAG = -Wall -Wextra
CFLAGS := -I$(INCLUDE_DIR) -fvisibility=hidden
ifeq ($(OSARCH), amd64)
CFLAGS += -fPIC -fPIE -pie -mno-80387 -mno-mmx -mno-3dnow \
-mno-red-zone -mno-sse -mno-sse2 \
-march=x86-64 -pipe -ffunction-sections \
-msoft-float -fno-builtin
else ifeq ($(OSARCH), i386)
CFLAGS += -fPIC -fPIE -pie -mno-80387 -mno-mmx -mno-3dnow \
-mno-red-zone -mno-sse -mno-sse2 -ffunction-sections \
-march=i386 -pipe -msoft-float -fno-builtin
else ifeq ($(OSARCH), aarch64)
CFLAGS += -pipe -fno-builtin -fPIC
endif
ifeq ($(DEBUG), 1)
CFLAGS += -DDEBUG -ggdb3 -O0 -fdiagnostics-color=always -fstack-usage
ifeq ($(OSARCH), amd64)
CFLAGS += -fverbose-asm
endif
ifneq ($(OSARCH), aarch64)
CFLAGS += -fstack-check
endif
LDFLAGS += -ggdb3 -O0
endif
FILENAME = ahci.drv
build: $(FILENAME)
mv $(FILENAME) ../../out/$(FILENAME)
$(FILENAME): $(OBJ)
$(info Linking $@)
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
%.o: %.c $(HEADERS)
$(info Compiling $<)
$(CC) $(CFLAGS) $(WARNCFLAG) -std=c17 -c $< -o $@
%.o: %.cpp $(HEADERS)
$(info Compiling $<)
$(CPP) $(CFLAGS) $(WARNCFLAG) -std=c++20 -fno-exceptions -fno-rtti -c $< -o $@
%.o: %.S
$(info Compiling $<)
$(AS) -o $@ $<
$(CC) $(DRIVER_LDFLAGS) $(OBJ) ../../out/dcrt0.o -L../../out -lkernel -o $@
clean:
rm -f file.map $(OBJ) $(STACK_USAGE_OBJ)

View File

@ -16,7 +16,7 @@
*/
#include <errno.h>
#include <block.h>
#include <device.h>
#include <regs.h>
#include <base.h>
#include <pci.h>
@ -634,9 +634,9 @@ public:
Identify();
Log("Port %d \"%x %x %x %x\" configured", PortNumber,
HBAPortPtr->Vendor[0], HBAPortPtr->Vendor[1],
HBAPortPtr->Vendor[2], HBAPortPtr->Vendor[3]);
KernelLog("Port %d \"%x %x %x %x\" configured", PortNumber,
HBAPortPtr->Vendor[0], HBAPortPtr->Vendor[1],
HBAPortPtr->Vendor[2], HBAPortPtr->Vendor[3]);
}
bool ReadWrite(uint64_t Sector, uint32_t SectorCount, uint8_t *Buffer, bool Write)
@ -644,7 +644,7 @@ public:
if (this->AHCIPortType == PortType::SATAPI &&
Write == true)
{
Log("SATAPI port does not support write.");
KernelLog("SATAPI port does not support write.");
return false;
}
@ -702,7 +702,7 @@ public:
if (Spin == 1000000)
{
Log("Port not responding.");
KernelLog("Port not responding.");
return false;
}
@ -715,8 +715,8 @@ public:
{
if (Spin > 100000000)
{
Log("Port %d not responding. (%d)",
this->PortNumber, TryCount);
KernelLog("Port %d not responding. (%d)",
this->PortNumber, TryCount);
Spin = 0;
TryCount++;
@ -730,7 +730,7 @@ public:
if (HBAPortPtr->InterruptStatus & HBA_PxIS_TFES)
{
Log("Error reading/writing (%d).", Write);
KernelLog("Error reading/writing (%d).", Write);
return false;
}
}
@ -766,12 +766,12 @@ public:
if (HBAPortPtr->InterruptStatus & HBA_PxIS_TFES)
{
Log("Error reading IDENTIFY command.");
KernelLog("Error reading IDENTIFY command.");
return;
}
if (IdentifyData->Signature != 0xA5)
Log("Port %d has no validity signature.", PortNumber);
KernelLog("Port %d has no validity signature.", PortNumber);
else
{
uint8_t *ptr = (uint8_t *)IdentifyData;
@ -780,11 +780,11 @@ public:
sum += ptr[i];
if (sum != 0)
{
Log("Port %d has invalid checksum.", PortNumber);
KernelLog("Port %d has invalid checksum.", PortNumber);
return;
}
else
Log("Port %d has valid checksum.", PortNumber);
KernelLog("Port %d has valid checksum.", PortNumber);
}
char *Model = (char *)this->IdentifyData->ModelNumber;
@ -796,11 +796,11 @@ public:
}
ModelSwap[40] = 0;
Log("Port %d \"%s\" identified", PortNumber,
ModelSwap);
Log("Port %d is %s (%d rotation rate)", PortNumber,
IdentifyData->NominalMediaRotationRate == 1 ? "SSD" : "HDD",
IdentifyData->NominalMediaRotationRate);
KernelLog("Port %d \"%s\" identified", PortNumber,
ModelSwap);
KernelLog("Port %d is %s (%d rotation rate)", PortNumber,
IdentifyData->NominalMediaRotationRate == 1 ? "SSD" : "HDD",
IdentifyData->NominalMediaRotationRate);
}
};
@ -839,26 +839,47 @@ PortType CheckPortType(HBAPort *Port)
}
}
size_t drvRead(dev_t, dev_t min,
uint8_t *Buffer, size_t Size, off_t Offset)
int __fs_Open(struct Inode *, int, mode_t) { return 0; }
int __fs_Close(struct Inode *) { return 0; }
ssize_t __fs_Read(struct Inode *Node, void *Buffer, size_t Size, off_t Offset)
{
bool ok = Ports[min]->ReadWrite(Offset / 512,
uint32_t(Size / 512),
Buffer,
false);
bool ok = Ports[Node->GetMinor()]->ReadWrite(Offset / 512,
uint32_t(Size / 512),
(uint8_t *)Buffer,
false);
return ok ? Size : 0;
}
size_t drvWrite(dev_t, dev_t min,
uint8_t *Buffer, size_t Size, off_t Offset)
ssize_t __fs_Write(struct Inode *Node, const void *Buffer, size_t Size, off_t Offset)
{
bool ok = Ports[min]->ReadWrite(Offset / 512,
uint32_t(Size / 512),
Buffer,
true);
bool ok = Ports[Node->GetMinor()]->ReadWrite(Offset / 512,
uint32_t(Size / 512),
(uint8_t *)Buffer,
true);
return ok ? Size : 0;
}
const struct InodeOperations BlockOps = {
.Lookup = nullptr,
.Create = nullptr,
.Remove = nullptr,
.Rename = nullptr,
.Read = __fs_Read,
.Write = __fs_Write,
.Truncate = nullptr,
.Open = __fs_Open,
.Close = __fs_Close,
.Ioctl = nullptr,
.ReadDir = nullptr,
.MkDir = nullptr,
.RmDir = nullptr,
.SymLink = nullptr,
.ReadLink = nullptr,
.Seek = nullptr,
.Stat = nullptr,
};
void OnInterruptReceived(TrapFrame *)
{
}
@ -880,10 +901,10 @@ EXTERNC int cxx_Probe()
0x2829, /* ICH8 */
0x07E0, /* SATA AHCI (VMware) */
PCI_END};
Devices = FindPCIDevices(VendorIDs, DeviceIDs);
Devices = GetPCIDevices(VendorIDs, DeviceIDs);
if (Devices == nullptr)
{
Log("No AHCI device found.");
KernelLog("No AHCI device found.");
return -ENODEV;
}
return 0;
@ -905,31 +926,28 @@ EXTERNC int cxx_Initialize()
HBAMemory *HBA = (HBAMemory *)(uintptr_t)GetBAR(5, ctx->Device);
uint32_t PortsImplemented = HBA->PortsImplemented;
Log("AHCI ports implemented: %x", PortsImplemented);
KernelLog("AHCI ports implemented: %x", PortsImplemented);
for (int i = 0; i < 32; i++)
{
if (PortCount > 64)
{
Log("There are more than 64 AHCI ports implemented");
KernelLog("There are more than 64 AHCI ports implemented");
break;
}
if (PortsImplemented & (1 << i))
{
Log("Port %d implemented", i);
KernelLog("Port %d implemented", i);
PortType portType = CheckPortType(&HBA->Ports[i]);
if (portType == PortType::SATA || portType == PortType::SATAPI)
{
KPrint("%s drive found at port %d", PortTypeName[portType], i);
KernelPrint("%s drive found at port %d", PortTypeName[portType], i);
Ports[PortCount] = new Port(portType, &HBA->Ports[i], PortCount);
dev_t ret = RegisterBlockDevice(ddt_SATA,
nullptr, nullptr,
drvRead, drvWrite,
nullptr);
dev_t ret = RegisterDevice(BLOCK_TYPE_HDD, &BlockOps);
if (ret != (dev_t)PortCount)
{
KPrint("Failed to register block device %d", ret);
return -EBADSLT;
KernelPrint("Failed to register block device %d", ret);
return -EBADF;
}
PortCount++;
@ -939,8 +957,8 @@ EXTERNC int cxx_Initialize()
{
if (portType != PortType::None)
{
KPrint("Unsupported drive type %s found at port %d",
PortTypeName[portType], i);
KernelPrint("Unsupported drive type %s found at port %d",
PortTypeName[portType], i);
}
}
}
@ -949,7 +967,7 @@ EXTERNC int cxx_Initialize()
ctx = (PCIArray *)ctx->Next;
}
Log("Initializing AHCI ports");
KernelLog("Initializing AHCI ports");
for (int i = 0; i < PortCount; i++)
Ports[i]->Configure();
@ -968,7 +986,7 @@ EXTERNC int cxx_Finalize()
do
{
UnregisterBlockDevice(PortCount, ddt_SATA);
UnregisterDevice(PortCount);
PortCount--;
} while (PortCount >= 0);

View File

@ -27,5 +27,5 @@ int DriverProbe() { return cxx_Probe(); }
DriverInfo("ahci",
"Advanced Host Controller Interface Driver",
"EnderIce2",
"0.1",
0, 0, 1,
"GPLv3");

View File

@ -1,83 +1,22 @@
# Config file
# Config files
include ../../../Makefile.conf
FILENAME = ata.drv
CC = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)gcc
CPP = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)g++
LD = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)ld
AS = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)as
OBJDUMP = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)objdump
include ../../config.mk
S_SOURCES = $(shell find ./ -type f -name '*.S')
C_SOURCES = $(shell find ./ -type f -name '*.c')
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp')
HEADERS = $(sort $(dir $(wildcard ../../include/*)))
OBJ = $(C_SOURCES:.c=.o) $(CPP_SOURCES:.cpp=.o) $(ASM_SOURCES:.asm=.o) $(S_SOURCES:.S=.o) $(PSF_SOURCES:.psf=.o) $(BMP_SOURCES:.bmp=.o)
OBJ = $(C_SOURCES:.c=.o) $(CPP_SOURCES:.cpp=.o) $(S_SOURCES:.S=.o)
STACK_USAGE_OBJ = $(C_SOURCES:.c=.su) $(CPP_SOURCES:.cpp=.su)
INCLUDE_DIR = ../../include
LIBS := ../../out/dcrt0.o -L../../out -ldriver
LDFLAGS := \
-fPIC -fPIE -pie \
-Wl,--no-dynamic-linker,-ztext,--no-warn-rwx-segment \
-nostdlib -nodefaultlibs -nolibc \
-zmax-page-size=0x1000 \
-Wl,-Map file.map -shared -fvisibility=hidden
WARNCFLAG = -Wall -Wextra
CFLAGS := -I$(INCLUDE_DIR) -fvisibility=hidden
ifeq ($(OSARCH), amd64)
CFLAGS += -fPIC -fPIE -pie -mno-80387 -mno-mmx -mno-3dnow \
-mno-red-zone -mno-sse -mno-sse2 \
-march=x86-64 -pipe -ffunction-sections \
-msoft-float -fno-builtin
else ifeq ($(OSARCH), i386)
CFLAGS += -fPIC -fPIE -pie -mno-80387 -mno-mmx -mno-3dnow \
-mno-red-zone -mno-sse -mno-sse2 -ffunction-sections \
-march=i386 -pipe -msoft-float -fno-builtin
else ifeq ($(OSARCH), aarch64)
CFLAGS += -pipe -fno-builtin -fPIC
endif
ifeq ($(DEBUG), 1)
CFLAGS += -DDEBUG -ggdb3 -O0 -fdiagnostics-color=always -fstack-usage
ifeq ($(OSARCH), amd64)
CFLAGS += -fverbose-asm
endif
ifneq ($(OSARCH), aarch64)
CFLAGS += -fstack-check
endif
LDFLAGS += -ggdb3 -O0
endif
FILENAME = ata.drv
build: $(FILENAME)
mv $(FILENAME) ../../out/$(FILENAME)
$(FILENAME): $(OBJ)
$(info Linking $@)
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
%.o: %.c $(HEADERS)
$(info Compiling $<)
$(CC) $(CFLAGS) $(WARNCFLAG) -std=c17 -c $< -o $@
%.o: %.cpp $(HEADERS)
$(info Compiling $<)
$(CPP) $(CFLAGS) $(WARNCFLAG) -std=c++20 -fno-exceptions -fno-rtti -c $< -o $@
%.o: %.S
$(info Compiling $<)
$(AS) -o $@ $<
$(CC) $(DRIVER_LDFLAGS) $(OBJ) ../../out/dcrt0.o -L../../out -lkernel -o $@
clean:
rm -f file.map $(OBJ) $(STACK_USAGE_OBJ)

View File

@ -72,5 +72,5 @@ int DriverProbe()
DriverInfo("ata",
"Advanced Technology Attachment Driver",
"EnderIce2",
"0.1",
0, 0, 1,
"GPLv3");