From ecb75b955e88f16368d389c77ddc4162dd3eac24 Mon Sep 17 00:00:00 2001 From: puffer Date: Mon, 26 Feb 2024 07:02:26 +0000 Subject: [PATCH] Fixed VFS errors and added PCI support --- src/filesystem/vfs.c | 336 +++++++++++++++++++++---------------------- src/filesystem/vfs.h | 17 +-- src/kernel/kernel.c | 4 +- src/system/pci/pci.c | 63 ++++++++ 4 files changed, 236 insertions(+), 184 deletions(-) create mode 100644 src/system/pci/pci.c diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index 75aea51..755af71 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -1,228 +1,214 @@ #include "vfs.h" - #include - #include #include #include VFS_t *init_vfs() { - VFS_t *vfs = (VFS_t *)malloc(sizeof(VFS_t)); - if (vfs == NULL) { - return NULL; - } + VFS_t *vfs = (VFS_t *)malloc(sizeof(VFS_t)); + if (vfs == NULL) { + return NULL; + } - vfs->drives = (drive_t *)malloc(MAX_DRIVES * sizeof(drive_t)); - if (vfs->drives == NULL) { - free(vfs); - return NULL; - } + vfs->drives = (drive_t *)malloc(MAX_DRIVES * sizeof(drive_t)); + if (vfs->drives == NULL) { + free(vfs); + return NULL; + } - vfs->address = (uint64_t)vfs; - vfs->size = sizeof(VFS_t); - vfs->actual_size = 0; + vfs->address = (uint64_t)vfs; + vfs->size = sizeof(VFS_t); + vfs->actual_size = 0; - dprintf("[\e[0;32mVFS\e[0m] Initialized VFS at 0x%08llX\n", vfs->address); + dprintf("[\e[0;32mVFS\e[0m] Initialized VFS at 0x%08llX\n", vfs->address); - return vfs; + return vfs; } -int mount_drive(VFS_t *vfs, uint64_t id, uint64_t address, char *label, - uint8_t type) { - if (vfs == NULL) { - dprintf("[\e[0;31mVFS\e[0m] NULL VFS passed!\n"); - return -1; - } +int mount_drive(VFS_t *vfs, int64_t id, uint64_t address, const char *label, uint8_t type) { + if (vfs == NULL) { + dprintf("[\e[0;31mVFS\e[0m] NULL VFS passed!\n"); + return -1; + } - if (id < 0 || id >= MAX_DRIVES) { - dprintf("[\e[0;31mVFS\e[0m] Invalid drive id!\n"); - return -1; - } + if (id < 0 || id >= MAX_DRIVES) { + dprintf("[\e[0;31mVFS\e[0m] Invalid drive id!\n"); + return -1; + } - if (address == 0) { - dprintf("[\e[0;31mVFS\e[0m] Invalid address passed!\n"); - return -1; - } + if (address == 0) { + dprintf("[\e[0;31mVFS\e[0m] Invalid address passed!\n"); + return -1; + } - drive_t temp_drive; - temp_drive.address = address; - temp_drive.size = sizeof(drive_t); - temp_drive.actual_size = sizeof(drive_t); - temp_drive.type = type; + drive_t temp_drive; + temp_drive.address = address; + temp_drive.size = sizeof(drive_t); + temp_drive.actual_size = sizeof(drive_t); + temp_drive.type = type; - mount_point_t temp_mount_point; - temp_mount_point.disk = vfs; - temp_mount_point.label = label; + mount_point_t temp_mount_point; + temp_mount_point.disk = vfs; + temp_mount_point.label = label; - temp_drive.mount_point = temp_mount_point; + temp_drive.mount_point = temp_mount_point; - temp_drive.id = id; + temp_drive.id = id; - vfs->drives[id] = temp_drive; + vfs->drives[id] = temp_drive; - dprintf( - "[\e[0;32mVFS\e[0m] Mounted drive with id 0x%08llX from 0x%08llX\n\e[0m", - temp_drive.id, temp_drive.address); + dprintf("[\e[0;32mVFS\e[0m] Mounted drive with id 0x%08llX from 0x%08llX\n\e[0m", temp_drive.id, temp_drive.address); - return 0; + return 0; } -int unmount_drive(VFS_t *vfs, uint64_t id) { - if (vfs == NULL) { - dprintf("[\e[0;31mVFS\e[0m] NULL VFS passed for unmounting!\n"); - return -1; - } +int unmount_drive(VFS_t *vfs, int64_t id) { + if (vfs == NULL) { + dprintf("[\e[0;31mVFS\e[0m] NULL VFS passed for unmounting!\n"); + return -1; + } - if (id < 0 || id >= MAX_DRIVES) { - dprintf("[\e[0;31mVFS\e[0m] Invalid drive id 0x%08llX\n", id); - return -1; - } + if (id < 0 || id >= MAX_DRIVES) { + dprintf("[\e[0;31mVFS\e[0m] Invalid drive id 0x%08llX\n", id); + return -1; + } - vfs->drives[id] = (drive_t){0}; + vfs->drives[id] = (drive_t){0}; - dprintf("[\e[0;32mVFS\e[0m] Unmounted drive with id 0x%08llX\n", id); + dprintf("[\e[0;32mVFS\e[0m] Unmounted drive with id 0x%08llX\n", id); - return 0; + return 0; } -vfs_op_status driver_read(VFS_t *disk, uint64_t id, const char *filepath, - char **buf) { - if (disk == NULL || buf == NULL) { - dprintf( - "[\e[0;31mVFS\e[0m] NULL VFS or buffer passed for reading drive!\n"); - return STATUS_INVALID_ARGUMENTS; - } - - drive_t *drive = &disk->drives[id]; - - if (drive == NULL) { - dprintf("[\e[0;31mVFS\e[0m] Drive with id 0x%08llX not found!\n", id); - return STATUS_DRIVE_NOT_FOUND; - } - - switch (drive->type) { - case DISK_TYPE_RAMDISK: - ramdisk_t *temp_rd = (ramdisk_t *)drive->address; - struct File *temp_file = rd_get_file(temp_rd, filepath); - - if (temp_file == NULL) { - dprintf("[\e[0;31mVFS\e[0m] File not found!\n"); - return STATUS_ERROR_FILE_NOT_FOUND; +vfs_op_status driver_read(VFS_t *disk, uint64_t id, const char *filepath, char **buf) { + if (disk == NULL || buf == NULL) { + dprintf("[\e[0;31mVFS\e[0m] NULL VFS or buffer passed for reading drive!\n"); + return STATUS_INVALID_ARGUMENTS; } - *buf = (char *)malloc(temp_file->size); - if (*buf == NULL) { - dprintf("[\e[0;31mVFS\e[0m] Failed to allocate memory for buffer\n"); - return STATUS_MALLOC_FAILURE; - } + drive_t *drive = &disk->drives[id]; - for (uint32_t i = 0; i < temp_file->size; ++i) { - (*buf)[i] = temp_file->content[i]; + if (drive == NULL) { + dprintf("[\e[0;31mVFS\e[0m] Drive with id 0x%08llX not found!\n", id); + return STATUS_DRIVE_NOT_FOUND; } - dprintf("[\e[0;32mVFS\e[0m] Successfully read from drive!\n"); - break; - default: - dprintf("[\e[0;31mVFS\e[0m] The drive has an invalid type? (%d)\n", - drive->type); - return STATUS_INVALID_DRIVE_TYPE; - } - return STATUS_OK; + switch (drive->type) { + case DISK_TYPE_RAMDISK: { + ramdisk_t *temp_rd = (ramdisk_t *)drive->address; + struct File *temp_file = rd_get_file(temp_rd, filepath); + + if (temp_file == NULL) { + dprintf("[\e[0;31mVFS\e[0m] File not found!\n"); + return STATUS_ERROR_FILE_NOT_FOUND; + } + + *buf = (char *)malloc(temp_file->size); + if (*buf == NULL) { + dprintf("[\e[0;31mVFS\e[0m] Failed to allocate memory for buffer\n"); + return STATUS_MALLOC_FAILURE; + } + + for (uint32_t i = 0; i < temp_file->size; ++i) { + (*buf)[i] = temp_file->content[i]; + } + + dprintf("[\e[0;32mVFS\e[0m] Successfully read from drive!\n"); + break; + } + default: + dprintf("[\e[0;31mVFS\e[0m] The drive has an invalid type? (%d)\n", drive->type); + return STATUS_INVALID_DRIVE_TYPE; + } + return STATUS_OK; } -vfs_op_status driver_write(VFS_t *disk, uint64_t id, const char *filepath, - const char *buf) { - - if (disk == NULL || buf == NULL) { - dprintf( - "[\e[0;31mVFS\e[0m] NULL VFS or buffer passed for writing to drive!\n"); - return STATUS_INVALID_ARGUMENTS; - } - - drive_t *drive = &disk->drives[id]; - - if (drive == NULL) { - dprintf("[\e[0;31mVFS\e[0m] Drive with id 0x%08llX not found!\n", id); - return STATUS_DRIVE_NOT_FOUND; - } +vfs_op_status driver_write(VFS_t *disk, uint64_t id, const char *filepath, const char *buf) { + if (disk == NULL || buf == NULL) { + dprintf("[\e[0;31mVFS\e[0m] NULL VFS or buffer passed for writing to drive!\n"); + return STATUS_INVALID_ARGUMENTS; + } - switch (drive->type) { - case DISK_TYPE_RAMDISK: - ramdisk_t *temp_rd = (ramdisk_t *)drive->address; - struct File *temp_file = rd_get_file(temp_rd, filepath); + drive_t *drive = &disk->drives[id]; - if (temp_file == NULL) { - dprintf("[\e[0;31mVFS\e[0m] File not found!\n"); - return STATUS_ERROR_FILE_NOT_FOUND; + if (drive == NULL) { + dprintf("[\e[0;31mVFS\e[0m] Drive with id 0x%08llX not found!\n", id); + return STATUS_DRIVE_NOT_FOUND; } - free(temp_file->content); - temp_file->content = strdup(buf); - - dprintf("[\e[0;32mVFS\e[0m] Successfully wrote to drive!\n"); - break; - default: - dprintf("[\e[0;31mVFS\e[0m] The drive has an invalid type? (%d)\n", - drive->type); - return STATUS_INVALID_DRIVE_TYPE; - } - return STATUS_OK; + switch (drive->type) { + case DISK_TYPE_RAMDISK: { + ramdisk_t *temp_rd = (ramdisk_t *)drive->address; + struct File *temp_file = rd_get_file(temp_rd, filepath); + + if (temp_file == NULL) { + dprintf("[\e[0;31mVFS\e[0m] File not found!\n"); + return STATUS_ERROR_FILE_NOT_FOUND; + } + + free(temp_file->content); + temp_file->content = strdup(buf); + + dprintf("[\e[0;32mVFS\e[0m] Successfully wrote to drive!\n"); + break; + } + default: + dprintf("[\e[0;31mVFS\e[0m] The drive has an invalid type? (%d)\n", drive->type); + return STATUS_INVALID_DRIVE_TYPE; + } + return STATUS_OK; } uint64_t get_drive_id_by_label(VFS_t *vfs, const char *label) { - if (vfs == NULL || label == NULL) { - dprintf( - "[\e[0;31mVFS\e[0m] NULL VFS or label passed for getting drive id!\n"); - return -1; - } + if (vfs == NULL || label == NULL) { + dprintf("[\e[0;31mVFS\e[0m] NULL VFS or label passed for getting drive id!\n"); + return -1; + } - for (int i = 0; i < MAX_DRIVES; i++) { - if (vfs->drives[i].mount_point.label != NULL && - strcmp(vfs->drives[i].mount_point.label, label) == 0) { - dprintf("[\e[0;32mVFS\e[0m] Found drive at \"%s\" with id 0x%08llX\n", - label, vfs->drives[i].id); - return vfs->drives[i].id; + for (int i = 0; i < MAX_DRIVES; i++) { + if (vfs->drives[i].mount_point.label != NULL && strcmp(vfs->drives[i].mount_point.label, label) == 0) { + dprintf("[\e[0;32mVFS\e[0m] Found drive at \"%s\" with id 0x%08llX\n", label, vfs->drives[i].id); + return vfs->drives[i].id; + } } - } - dprintf("[\e[0;31mVFS\e[0m] Drive with label \"%s\" not found!\n", label); - return -1; + dprintf("[\e[0;31mVFS\e[0m] Drive with label \"%s\" not found!\n", label); + return -1; } size_t vfs_get_file_size(VFS_t *vfs, uint64_t id, const char *filepath) { - if (vfs == NULL || filepath == NULL) { - dprintf("[\e[0;31mVFS\e[0m] Invalid arguments: vfs or filepath is NULL\n"); - return 0; - } + if (vfs == NULL || filepath == NULL) { + dprintf("[\e[0;31mVFS\e[0m] Invalid arguments: vfs or filepath is NULL\n"); + return 0; + } - drive_t *drive = &vfs->drives[id]; - if (drive == NULL) { - dprintf("[\e[0;31mVFS\e[0m] Invalid drive: id %llu\n", id); - return 0; - } - - switch (drive->type) { - case DISK_TYPE_RAMDISK: { - ramdisk_t *rd = (ramdisk_t *)(uintptr_t)(drive->address); - if (rd == NULL) { - dprintf("[\e[0;31mVFS\e[0m] RAM disk not initialized\n"); - return 0; - } - - for (unsigned int i = 0; i < rd->files; i++) { - if (strcmp(rd->content->files[i].name, filepath) == 0) { - dprintf("[\e[0;32mVFS\e[0m] File found: %s, Size: %u bytes\n", filepath, - rd->content->files[i].size); - return rd->content->files[i].size; - } - } - dprintf("[\e[0;31mVFS\e[0m] File not found: %s\n", filepath); - break; - } - default: - dprintf("[\e[0;31mVFS\e[0m] Unsupported drive type: %d\n", drive->type); - return 0; - } + drive_t *drive = &vfs->drives[id]; + if (drive == NULL) { + dprintf("[\e[0;31mVFS\e[0m] Invalid drive: id %llu\n", id); + return 0; + } + + switch (drive->type) { + case DISK_TYPE_RAMDISK: { + ramdisk_t *rd = (ramdisk_t *)(uintptr_t)(drive->address); + if (rd == NULL) { + dprintf("[\e[0;31mVFS\e[0m] RAM disk not initialized\n"); + return 0; + } + + for (unsigned int i = 0; i < rd->files; i++) { + if (strcmp(rd->content->files[i].name, filepath) == 0) { + dprintf("[\e[0;32mVFS\e[0m] File found: %s, Size: %u bytes\n", filepath, rd->content->files[i].size); + return rd->content->files[i].size; + } + } + dprintf("[\e[0;31mVFS\e[0m] File not found: %s\n", filepath); + break; + } + default: + dprintf("[\e[0;31mVFS\e[0m] Unsupported drive type: %d\n", drive->type); + return 0; + } - return 0; + return 0; } diff --git a/src/filesystem/vfs.h b/src/filesystem/vfs.h index 1f5eca9..91bd9e4 100644 --- a/src/filesystem/vfs.h +++ b/src/filesystem/vfs.h @@ -19,9 +19,11 @@ typedef enum { STATUS_UNKNOWN_ERROR } vfs_op_status; +typedef struct VFS_t VFS_t; // Forward declaration + typedef struct { - struct VFS_t *disk; - char *label; + VFS_t *disk; + const char *label; } mount_point_t; typedef struct { @@ -34,19 +36,18 @@ typedef struct { uint64_t actual_size; } drive_t; -typedef struct VFS_t { +struct VFS_t { drive_t *drives; uint64_t address; uint64_t size; uint64_t actual_size; -} VFS_t; +}; VFS_t *init_vfs(); -int mount_drive(VFS_t *vfs, uint64_t id, uint64_t address, char *label, - uint8_t type); -int unmount_drive(VFS_t *disk, uint64_t id); +int mount_drive(VFS_t *vfs, int64_t id, uint64_t address, const char *label, uint8_t type); +int unmount_drive(VFS_t *vfs, int64_t id); vfs_op_status driver_read(VFS_t *disk, uint64_t id, const char *filepath, char **buf); @@ -56,4 +57,4 @@ vfs_op_status driver_write(VFS_t *disk, uint64_t id, const char *filepath, uint64_t get_drive_id_by_label(VFS_t *vfs, const char *label); size_t vfs_get_file_size(VFS_t *vfs, uint64_t id, const char *filepath); -#endif // __VFS_H__ \ No newline at end of file +#endif // __VFS_H__ diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 3fd8cbc..b695a68 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -24,6 +24,7 @@ return codes) #include #include #include +#include // Corelib includes #include @@ -32,6 +33,7 @@ return codes) #include int main() { - init_wm(); + register_pci(); + // init_wm(); return KERNEL_QUIT_HANG; } \ No newline at end of file diff --git a/src/system/pci/pci.c b/src/system/pci/pci.c new file mode 100644 index 0000000..d01194d --- /dev/null +++ b/src/system/pci/pci.c @@ -0,0 +1,63 @@ +#include "pci.h" +#include + +uint32_t read_pci(uint16_t bus, uint16_t device, uint16_t function, uint32_t regoffset) { + uint32_t id = 0x1 << 31 | ((bus & 0xFF) << 16) | ((device & 0x1F) << 11) | ((function & 0x07) << 8) | (regoffset & 0xFC); + outb32(0xCF8, id); + uint32_t result = inb32(0xCFC); + return result >> ((regoffset % 4) << 3); +} + +void write_pci(uint16_t bus, uint16_t device, uint16_t function, uint32_t regoffset, uint32_t data) { + uint32_t id = 0x1 << 31 | ((bus & 0xFF) << 16) | ((device & 0x1F) << 11) | ((function & 0x07) << 8) | (regoffset & 0xFC); //we construct an ID like in the read function + outb32(0xCF8, id); + outb32(0xCFC, data); +} + +device_descriptor get_device_descriptor(uint16_t bus, uint16_t device, uint16_t function) { + device_descriptor result; + + result.bus = bus; + result.device = device; + result.function = function; + + result.vendor_id = read_pci(bus, device, function, 0x00); + result.device_id = read_pci(bus, device, function, 0x02); + + result.class_id = read_pci(bus, device, function, 0x0b); + result.subclass_id = read_pci(bus, device, function, 0x0a); + result.interface_id = read_pci(bus, device, function, 0x09); + + result.revision = read_pci(bus, device, function, 0x08); + result.interrupt = read_pci(bus, device, function, 0x3c); + + return result; +} + +void register_pci() { + for(int bus = 0; bus < 8; bus++) + { + for(int device = 0; device < 32; device++) + { + for(int function = 0; function < 8; function++) + { + device_descriptor desc = get_device_descriptor(bus, device, function); + + if(desc.vendor_id == 0x0000 || desc.vendor_id == 0xFFFF) + continue; + + dprintf("PCI bus: %d\n", (uint8_t)(bus & 0xFF)); + dprintf(" - Device: %d\n", (uint8_t)(device & 0xFF)); + dprintf(" - Function: %d\n", ((uint8_t)((function & 0xFF)))); + dprintf(" - Vendor ID: %d%d\n", (uint8_t)((desc.vendor_id & 0xFF00) >> 8), (uint8_t)(desc.vendor_id & 0xFF)); + dprintf(" - Device ID: %d%d\n", (uint8_t)((desc.device_id & 0xFF00) >> 8), (uint8_t)(desc.device_id & 0xFF)); + if((desc.class_id == 0x0C) && (desc.subclass_id == 0x03)) + { + dprintf("USB device ^^\n"); + } else { + dprintf("\n"); + } + } + } + } +} \ No newline at end of file