From 41426aef43726f99585f6858b7a03f730411e261 Mon Sep 17 00:00:00 2001 From: pilote23 Date: Wed, 23 Jul 2025 14:41:45 +0200 Subject: [PATCH] replaced printf with debug functions --- src/kernel/blockdev.cpp | 21 ++++--- src/kernel/fat32.cpp | 130 +++++++++++++++++++-------------------- src/kernel/fat32_vfs.cpp | 45 +++++++------- src/kernel/ide.cpp | 31 +++++----- src/kernel/isr.cpp | 2 +- src/kernel/vfs.cpp | 81 ++++++++++++------------ 6 files changed, 155 insertions(+), 155 deletions(-) diff --git a/src/kernel/blockdev.cpp b/src/kernel/blockdev.cpp index 3a43bdb..5de6908 100644 --- a/src/kernel/blockdev.cpp +++ b/src/kernel/blockdev.cpp @@ -1,5 +1,6 @@ #include "kernel/blockdev.h" #include "kernel/ide.h" +#include "kernel/debug.h" #include #include @@ -7,7 +8,7 @@ static blockdev_info_t devices[MAX_BLOCK_DEVICES]; static uint8_t device_count = 0; int blockdev_init(void) { - printf("[BLOCKDEV] Initializing block device subsystem\n"); + debug("[BLOCKDEV] Initializing block device subsystem"); device_count = 0; memset(devices, 0, sizeof(devices)); @@ -33,18 +34,18 @@ int blockdev_init(void) { } } - printf("[BLOCKDEV] Registered %d block devices\n", device_count); + success("[BLOCKDEV] Registered %d block devices", device_count); return device_count; } int blockdev_register(uint8_t type, uint8_t device_id, blockdev_info_t* info) { if (device_count >= MAX_BLOCK_DEVICES) { - printf("[BLOCKDEV] Maximum devices reached\n"); + error("[BLOCKDEV] Maximum devices reached"); return BLOCKDEV_ERROR; } devices[device_count] = *info; - printf("[BLOCKDEV] Registered device %d: %s (%u sectors, %u bytes/sector)\n", + debug("[BLOCKDEV] Registered device %d: %s (%u sectors, %u bytes/sector)", device_count, info->name, info->sector_count, info->sector_size); device_count++; @@ -53,18 +54,18 @@ int blockdev_register(uint8_t type, uint8_t device_id, blockdev_info_t* info) { int blockdev_read(uint8_t device, uint32_t sector, uint8_t count, void* buffer) { if (device >= device_count || !devices[device].present) { - printf("[BLOCKDEV] Invalid device: %d\n", device); + error("[BLOCKDEV] Invalid device: %d", device); return BLOCKDEV_NOT_FOUND; } blockdev_info_t* dev = &devices[device]; if (sector >= dev->sector_count) { - printf("[BLOCKDEV] Sector %u out of range (max: %u)\n", sector, dev->sector_count - 1); + error("[BLOCKDEV] Sector %u out of range (max: %u)", sector, dev->sector_count - 1); return BLOCKDEV_ERROR; } - printf("[BLOCKDEV] Reading %d sectors from sector %u on device %d (%s)\n", + debug("[BLOCKDEV] Reading %d sectors from sector %u on device %d (%s)", count, sector, device, dev->name); switch (dev->type) { @@ -72,7 +73,7 @@ int blockdev_read(uint8_t device, uint32_t sector, uint8_t count, void* buffer) return ide_read_sectors(dev->device_id, sector, count, (uint16_t*)buffer); default: - printf("[BLOCKDEV] Unsupported device type: %d\n", dev->type); + error("[BLOCKDEV] Unsupported device type: %d", dev->type); return BLOCKDEV_ERROR; } } @@ -101,10 +102,10 @@ blockdev_info_t* blockdev_get_info(uint8_t device) { } int blockdev_list_devices(void) { - printf("[BLOCKDEV] Available block devices:\n"); + debug("[BLOCKDEV] Available block devices:"); for (int i = 0; i < device_count; i++) { if (devices[i].present) { - printf(" %d: %s - %u sectors (%u MB)\n", + debug(" %d: %s - %u sectors (%u MB)", i, devices[i].name, devices[i].sector_count, (devices[i].sector_count * devices[i].sector_size) / (1024 * 1024)); } diff --git a/src/kernel/fat32.cpp b/src/kernel/fat32.cpp index 9c7c107..a018426 100644 --- a/src/kernel/fat32.cpp +++ b/src/kernel/fat32.cpp @@ -1,6 +1,7 @@ #include "kernel/fat32.h" #include "kernel/blockdev.h" #include "kernel/heap.h" +#include "kernel/debug.h" #include #include @@ -9,7 +10,7 @@ static fat32_file_t open_files[FAT32_MAX_OPEN_FILES]; static uint8_t mounted = 0; int fat32_init(void) { - printf("[FAT32] Initializing FAT32 filesystem support\n"); + debug("[FAT32] Initializing FAT32 filesystem support"); memset(&fs_info, 0, sizeof(fs_info)); memset(open_files, 0, sizeof(open_files)); @@ -21,36 +22,36 @@ int fat32_init(void) { static int fat32_validate_boot_sector(fat32_boot_sector_t* boot) { // Check signature if (boot->signature != FAT32_SIGNATURE) { - printf("[FAT32] Invalid boot sector signature: 0x%x\n", boot->signature); + error("[FAT32] Invalid boot sector signature: 0x%x", boot->signature); return -1; } // Check if it's FAT32 if (boot->fat_size_16 != 0) { - printf("[FAT32] Not a FAT32 filesystem (fat_size_16 != 0)\n"); + error("[FAT32] Not a FAT32 filesystem (fat_size_16 != 0)"); return -1; } if (boot->fat_size_32 == 0) { - printf("[FAT32] Invalid FAT32 (fat_size_32 == 0)\n"); + error("[FAT32] Invalid FAT32 (fat_size_32 == 0)"); return -1; } // Check bytes per sector if (boot->bytes_per_sector != 512) { - printf("[FAT32] Unsupported sector size: %d\n", boot->bytes_per_sector); + error("[FAT32] Unsupported sector size: %d", boot->bytes_per_sector); return -1; } - printf("[FAT32] Boot sector validation passed\n"); + success("[FAT32] Boot sector validation passed"); return 0; } int fat32_mount(uint8_t device_id) { - printf("[FAT32] Mounting FAT32 filesystem on device %d\n", device_id); + debug("[FAT32] Mounting FAT32 filesystem on device %d", device_id); if (mounted) { - printf("[FAT32] Filesystem already mounted\n"); + error("[FAT32] Filesystem already mounted"); return -1; } @@ -58,22 +59,18 @@ int fat32_mount(uint8_t device_id) { fat32_boot_sector_t boot_sector; int result = blockdev_read(device_id, 0, 1, &boot_sector); if (result != 0) { - printf("[FAT32] Failed to read boot sector: %d\n", result); + error("[FAT32] Failed to read boot sector: %d", result); return -1; } - printf("[FAT32] Boot sector read successfully\n"); - printf("[FAT32] OEM Name: "); - for (int i = 0; i < 8; i++) { - printf("%c", boot_sector.oem_name[i]); - } - printf("\n"); - printf("[FAT32] Bytes per sector: %d\n", boot_sector.bytes_per_sector); - printf("[FAT32] Sectors per cluster: %d\n", boot_sector.sectors_per_cluster); - printf("[FAT32] Reserved sectors: %d\n", boot_sector.reserved_sectors); - printf("[FAT32] Number of FATs: %d\n", boot_sector.num_fats); - printf("[FAT32] FAT size: %u sectors\n", boot_sector.fat_size_32); - printf("[FAT32] Root cluster: %u\n", boot_sector.root_cluster); + success("[FAT32] Boot sector read successfully"); + debug("[FAT32] OEM Name: %.8s", boot_sector.oem_name); + debug("[FAT32] Bytes per sector: %d", boot_sector.bytes_per_sector); + debug("[FAT32] Sectors per cluster: %d", boot_sector.sectors_per_cluster); + debug("[FAT32] Reserved sectors: %d", boot_sector.reserved_sectors); + debug("[FAT32] Number of FATs: %d", boot_sector.num_fats); + debug("[FAT32] FAT size: %u sectors", boot_sector.fat_size_32); + debug("[FAT32] Root cluster: %u", boot_sector.root_cluster); // Validate boot sector if (fat32_validate_boot_sector(&boot_sector) != 0) { @@ -98,29 +95,29 @@ int fat32_mount(uint8_t device_id) { uint32_t data_sectors = total_sectors - fs_info.data_start_sector; fs_info.total_clusters = data_sectors / fs_info.sectors_per_cluster; - printf("[FAT32] FAT starts at sector: %u\n", fs_info.fat_start_sector); - printf("[FAT32] Data starts at sector: %u\n", fs_info.data_start_sector); - printf("[FAT32] Total clusters: %u\n", fs_info.total_clusters); + debug("[FAT32] FAT starts at sector: %u", fs_info.fat_start_sector); + debug("[FAT32] Data starts at sector: %u", fs_info.data_start_sector); + debug("[FAT32] Total clusters: %u", fs_info.total_clusters); // Allocate memory for FAT table (simplified - load entire FAT) uint32_t fat_bytes = fs_info.fat_size * fs_info.bytes_per_sector; fs_info.fat_table = (uint32_t*)kmalloc(fat_bytes); if (!fs_info.fat_table) { - printf("[FAT32] Failed to allocate memory for FAT table\n"); + error("[FAT32] Failed to allocate memory for FAT table"); return -1; } // Read FAT table - printf("[FAT32] Reading FAT table (%u sectors)...\n", fs_info.fat_size); + debug("[FAT32] Reading FAT table (%u sectors)...", fs_info.fat_size); result = blockdev_read(device_id, fs_info.fat_start_sector, fs_info.fat_size, fs_info.fat_table); if (result != 0) { - printf("[FAT32] Failed to read FAT table: %d\n", result); + error("[FAT32] Failed to read FAT table: %d", result); kfree(fs_info.fat_table); return -1; } - printf("[FAT32] FAT32 filesystem mounted successfully\n"); + success("[FAT32] FAT32 filesystem mounted successfully"); mounted = 1; return 0; } @@ -136,7 +133,7 @@ int fat32_unmount(void) { } mounted = 0; - printf("[FAT32] Filesystem unmounted\n"); + debug("[FAT32] Filesystem unmounted"); return 0; } @@ -215,13 +212,13 @@ int fat32_list_directory(uint32_t dir_cluster, fat32_file_info_t* files, int max return -1; } - printf("[FAT32] Listing directory cluster %u\n", dir_cluster); + debug("[FAT32] Listing directory cluster %u", dir_cluster); // Allocate buffer for one cluster uint32_t cluster_size = fs_info.sectors_per_cluster * fs_info.bytes_per_sector; uint8_t* cluster_buffer = (uint8_t*)kmalloc(cluster_size); if (!cluster_buffer) { - printf("[FAT32] Failed to allocate cluster buffer\n"); + error("[FAT32] Failed to allocate cluster buffer"); return -1; } @@ -232,7 +229,7 @@ int fat32_list_directory(uint32_t dir_cluster, fat32_file_info_t* files, int max while (current_cluster < FAT32_END_CLUSTER && file_count < max_files) { // Read the cluster if (fat32_read_cluster(current_cluster, cluster_buffer) != 0) { - printf("[FAT32] Failed to read cluster %u\n", current_cluster); + error("[FAT32] Failed to read cluster %u", current_cluster); break; } @@ -255,11 +252,10 @@ int fat32_list_directory(uint32_t dir_cluster, fat32_file_info_t* files, int max file_count++; // Print entry info - printf(" %s", info.filename); - if (info.attributes & FAT32_ATTR_DIRECTORY) { - printf("/"); - } - printf(" (%u bytes, cluster %u)\n", info.size, info.cluster); + debug(" %s%s (%u bytes, cluster %u)", + info.filename, + (info.attributes & FAT32_ATTR_DIRECTORY) ? "/" : "", + info.size, info.cluster); } } @@ -269,7 +265,7 @@ int fat32_list_directory(uint32_t dir_cluster, fat32_file_info_t* files, int max done: kfree(cluster_buffer); - printf("[FAT32] Found %d entries\n", file_count); + debug("[FAT32] Found %d entries", file_count); return file_count; } @@ -292,14 +288,14 @@ uint32_t fat32_find_file(uint32_t dir_cluster, const char* filename) { int fat32_open(const char* path) { if (!mounted) { - printf("[FAT32] No filesystem mounted\n"); + error("[FAT32] No filesystem mounted"); return -1; } // Use new path resolution to support subdirectories uint32_t file_cluster = fat32_find_file_by_path(path); if (file_cluster == 0) { - printf("[FAT32] File not found: %s\n", path); + error("[FAT32] File not found: %s", path); return -1; } @@ -322,13 +318,13 @@ int fat32_open(const char* path) { } } - printf("[FAT32] Opened file %s (fd=%d, cluster=%u, size=%u)\n", - path, fd, file_cluster, open_files[fd].file_size); + success("[FAT32] Opened file %s (fd=%d, cluster=%u, size=%u)", + path, fd, file_cluster, open_files[fd].file_size); return fd; } } - printf("[FAT32] No free file descriptors\n"); + error("[FAT32] No free file descriptors"); return -1; } @@ -342,8 +338,8 @@ int fat32_read(int fd, void* buffer, size_t size) { size_t bytes_read = 0; uint32_t cluster_size = fs_info.sectors_per_cluster * fs_info.bytes_per_sector; - printf("[FAT32] Reading %u bytes from fd %d (pos=%u, size=%u)\n", - size, fd, file->position, file->file_size); + debug("[FAT32] Reading %u bytes from fd %d (pos=%u, size=%u)", + size, fd, file->position, file->file_size); // Don't read past end of file if (file->position >= file->file_size) { @@ -356,14 +352,14 @@ int fat32_read(int fd, void* buffer, size_t size) { // Allocate cluster buffer uint8_t* cluster_buffer = (uint8_t*)kmalloc(cluster_size); if (!cluster_buffer) { - printf("[FAT32] Failed to allocate cluster buffer\n"); + error("[FAT32] Failed to allocate cluster buffer"); return -1; } while (bytes_read < size && file->current_cluster < FAT32_END_CLUSTER) { // Read current cluster if (fat32_read_cluster(file->current_cluster, cluster_buffer) != 0) { - printf("[FAT32] Failed to read cluster %u\n", file->current_cluster); + error("[FAT32] Failed to read cluster %u", file->current_cluster); kfree(cluster_buffer); return -1; } @@ -388,7 +384,7 @@ int fat32_read(int fd, void* buffer, size_t size) { } kfree(cluster_buffer); - printf("[FAT32] Read %u bytes\n", bytes_read); + debug("[FAT32] Read %u bytes", bytes_read); return bytes_read; } @@ -427,26 +423,26 @@ int fat32_seek(int fd, uint32_t position) { void fat32_close(int fd) { if (fd >= 0 && fd < FAT32_MAX_OPEN_FILES && open_files[fd].in_use) { open_files[fd].in_use = 0; - printf("[FAT32] Closed file descriptor %d\n", fd); + debug("[FAT32] Closed file descriptor %d", fd); } } int fat32_get_fs_info(void) { if (!mounted) { - printf("[FAT32] No filesystem mounted\n"); + error("[FAT32] No filesystem mounted"); return -1; } - printf("[FAT32] Filesystem Information:\n"); - printf(" Device: %d\n", fs_info.device_id); - printf(" Bytes per sector: %u\n", fs_info.bytes_per_sector); - printf(" Sectors per cluster: %u\n", fs_info.sectors_per_cluster); - printf(" Reserved sectors: %u\n", fs_info.reserved_sectors); - printf(" Number of FATs: %u\n", fs_info.num_fats); - printf(" FAT size: %u sectors\n", fs_info.fat_size); - printf(" Root cluster: %u\n", fs_info.root_cluster); - printf(" Data start sector: %u\n", fs_info.data_start_sector); - printf(" Total clusters: %u\n", fs_info.total_clusters); + debug("[FAT32] Filesystem Information:"); + debug(" Device: %d", fs_info.device_id); + debug(" Bytes per sector: %u", fs_info.bytes_per_sector); + debug(" Sectors per cluster: %u", fs_info.sectors_per_cluster); + debug(" Reserved sectors: %u", fs_info.reserved_sectors); + debug(" Number of FATs: %u", fs_info.num_fats); + debug(" FAT size: %u sectors", fs_info.fat_size); + debug(" Root cluster: %u", fs_info.root_cluster); + debug(" Data start sector: %u", fs_info.data_start_sector); + debug(" Total clusters: %u", fs_info.total_clusters); return 0; } @@ -464,7 +460,7 @@ uint32_t fat32_resolve_path(const char* path, char* filename) { return 0; } - printf("[FAT32] Resolving path: %s\n", path); + debug("[FAT32] Resolving path: %s", path); // Start from root directory uint32_t current_cluster = fs_info.root_cluster; @@ -494,26 +490,26 @@ uint32_t fat32_resolve_path(const char* path, char* filename) { strncpy(filename, start, FAT32_MAX_FILENAME); filename[FAT32_MAX_FILENAME] = '\0'; } - printf("[FAT32] Final component: %s (directory cluster: %u)\n", start, current_cluster); + debug("[FAT32] Final component: %s (directory cluster: %u)", start, current_cluster); return current_cluster; } // Extract directory component size_t len = end - start; if (len > FAT32_MAX_FILENAME) { - printf("[FAT32] Directory name too long: %.*s\n", (int)len, start); + error("[FAT32] Directory name too long: %.*s", (int)len, start); return 0; } strncpy(component, start, len); component[len] = '\0'; - printf("[FAT32] Looking for directory: %s in cluster %u\n", component, current_cluster); + debug("[FAT32] Looking for directory: %s in cluster %u", component, current_cluster); // Find the directory in current cluster uint32_t dir_cluster = fat32_find_file(current_cluster, component); if (dir_cluster == 0) { - printf("[FAT32] Directory not found: %s\n", component); + error("[FAT32] Directory not found: %s", component); return 0; } @@ -532,7 +528,7 @@ uint32_t fat32_resolve_path(const char* path, char* filename) { } if (!is_directory) { - printf("[FAT32] Path component is not a directory: %s\n", component); + error("[FAT32] Path component is not a directory: %s", component); return 0; } @@ -558,6 +554,6 @@ uint32_t fat32_find_file_by_path(const char* path) { return 0; } - printf("[FAT32] Looking for file '%s' in directory cluster %u\n", filename, dir_cluster); + debug("[FAT32] Looking for file '%s' in directory cluster %u", filename, dir_cluster); return fat32_find_file(dir_cluster, filename); } \ No newline at end of file diff --git a/src/kernel/fat32_vfs.cpp b/src/kernel/fat32_vfs.cpp index 1f727b2..6972f0d 100644 --- a/src/kernel/fat32_vfs.cpp +++ b/src/kernel/fat32_vfs.cpp @@ -1,6 +1,7 @@ #include "kernel/vfs.h" #include "kernel/fat32.h" #include "kernel/heap.h" +#include "kernel/debug.h" #include #include @@ -9,7 +10,7 @@ static int fat32_vfs_open(vfs_mount_t* mount, const char* path, vfs_file_t* file) { (void)mount; // Mount info available if needed - printf("[FAT32-VFS] Opening file: %s\n", path); + debug("[FAT32-VFS] Opening file: %s", path); // FAT32 currently only supports root directory files // Remove leading slash for FAT32 compatibility @@ -20,13 +21,13 @@ static int fat32_vfs_open(vfs_mount_t* mount, const char* path, vfs_file_t* file // Skip empty filename (root directory) if (filename[0] == '\0') { - printf("[FAT32-VFS] Cannot open root directory as file\n"); + error("[FAT32-VFS] Cannot open root directory as file"); return VFS_ERROR; } int fat32_fd = fat32_open(filename); if (fat32_fd < 0) { - printf("[FAT32-VFS] Failed to open file: %s\n", filename); + error("[FAT32-VFS] Failed to open file: %s", filename); return VFS_NOT_FOUND; } @@ -35,7 +36,7 @@ static int fat32_vfs_open(vfs_mount_t* mount, const char* path, vfs_file_t* file file->position = 0; file->in_use = 1; - printf("[FAT32-VFS] Successfully opened file: %s (fd=%d)\n", filename, fat32_fd); + success("[FAT32-VFS] Successfully opened file: %s (fd=%d)", filename, fat32_fd); return VFS_SUCCESS; } @@ -49,9 +50,9 @@ static int fat32_vfs_read(vfs_file_t* file, void* buffer, size_t size) { if (bytes_read >= 0) { file->position += bytes_read; - printf("[FAT32-VFS] Read %d bytes from FAT32 file\n", bytes_read); + debug("[FAT32-VFS] Read %d bytes from FAT32 file", bytes_read); } else { - printf("[FAT32-VFS] Failed to read from FAT32 file\n"); + error("[FAT32-VFS] Failed to read from FAT32 file"); } return bytes_read; @@ -62,7 +63,7 @@ static int fat32_vfs_write(vfs_file_t* file, const void* buffer, size_t size) { (void)buffer; (void)size; - printf("[FAT32-VFS] Write not supported (read-only filesystem)\n"); + error("[FAT32-VFS] Write not supported (read-only filesystem)"); return VFS_ERROR; } @@ -76,10 +77,10 @@ static int fat32_vfs_seek(vfs_file_t* file, uint32_t position) { if (result == 0) { file->position = position; - printf("[FAT32-VFS] Seeked to position %u\n", position); + debug("[FAT32-VFS] Seeked to position %u", position); return VFS_SUCCESS; } else { - printf("[FAT32-VFS] Failed to seek to position %u\n", position); + error("[FAT32-VFS] Failed to seek to position %u", position); return VFS_ERROR; } } @@ -89,7 +90,7 @@ static void fat32_vfs_close(vfs_file_t* file) { int fat32_fd = (int)file->fs_handle; fat32_close(fat32_fd); - printf("[FAT32-VFS] Closed FAT32 file (fd=%d)\n", fat32_fd); + debug("[FAT32-VFS] Closed FAT32 file (fd=%d)", fat32_fd); file->in_use = 0; file->fs_handle = 0; file->position = 0; @@ -99,12 +100,12 @@ static void fat32_vfs_close(vfs_file_t* file) { static int fat32_vfs_readdir(vfs_mount_t* mount, const char* path, vfs_dirent_t* entries, int max_entries) { (void)mount; // Mount info available if needed - printf("[FAT32-VFS] Reading directory: %s\n", path); + debug("[FAT32-VFS] Reading directory: %s", path); // Allocate temporary buffer for FAT32 file info fat32_file_info_t* fat32_entries = (fat32_file_info_t*)kmalloc(max_entries * sizeof(fat32_file_info_t)); if (!fat32_entries) { - printf("[FAT32-VFS] Failed to allocate memory for directory listing\n"); + error("[FAT32-VFS] Failed to allocate memory for directory listing"); return VFS_ERROR; } @@ -112,14 +113,14 @@ static int fat32_vfs_readdir(vfs_mount_t* mount, const char* path, vfs_dirent_t* char filename[FAT32_MAX_FILENAME + 1]; uint32_t dir_cluster = fat32_resolve_path(path, filename); if (dir_cluster == 0) { - printf("[FAT32-VFS] Directory not found: %s\n", path); + error("[FAT32-VFS] Directory not found: %s", path); kfree(fat32_entries); return VFS_ERROR; } // If there's a filename, this is not a directory path if (strlen(filename) > 0) { - printf("[FAT32-VFS] Path is not a directory: %s\n", path); + error("[FAT32-VFS] Path is not a directory: %s", path); kfree(fat32_entries); return VFS_ERROR; } @@ -127,7 +128,7 @@ static int fat32_vfs_readdir(vfs_mount_t* mount, const char* path, vfs_dirent_t* int count = fat32_list_directory(dir_cluster, fat32_entries, max_entries); if (count < 0) { - printf("[FAT32-VFS] Failed to list FAT32 directory\n"); + error("[FAT32-VFS] Failed to list FAT32 directory"); kfree(fat32_entries); return VFS_ERROR; } @@ -142,7 +143,7 @@ static int fat32_vfs_readdir(vfs_mount_t* mount, const char* path, vfs_dirent_t* } kfree(fat32_entries); - printf("[FAT32-VFS] Found %d entries in FAT32 directory\n", count); + success("[FAT32-VFS] Found %d entries in FAT32 directory", count); return count; } @@ -150,7 +151,7 @@ static int fat32_vfs_mkdir(vfs_mount_t* mount, const char* path) { (void)mount; (void)path; - printf("[FAT32-VFS] mkdir not supported (read-only filesystem)\n"); + error("[FAT32-VFS] mkdir not supported (read-only filesystem)"); return VFS_ERROR; } @@ -158,7 +159,7 @@ static int fat32_vfs_rmdir(vfs_mount_t* mount, const char* path) { (void)mount; (void)path; - printf("[FAT32-VFS] rmdir not supported (read-only filesystem)\n"); + error("[FAT32-VFS] rmdir not supported (read-only filesystem)"); return VFS_ERROR; } @@ -166,7 +167,7 @@ static int fat32_vfs_create(vfs_mount_t* mount, const char* path) { (void)mount; (void)path; - printf("[FAT32-VFS] create not supported (read-only filesystem)\n"); + error("[FAT32-VFS] create not supported (read-only filesystem)"); return VFS_ERROR; } @@ -174,7 +175,7 @@ static int fat32_vfs_remove(vfs_mount_t* mount, const char* path) { (void)mount; (void)path; - printf("[FAT32-VFS] remove not supported (read-only filesystem)\n"); + error("[FAT32-VFS] remove not supported (read-only filesystem)"); return VFS_ERROR; } @@ -251,11 +252,11 @@ vfs_operations_t* fat32_get_vfs_ops(void) { // Function to mount FAT32 via VFS int fat32_vfs_mount(const char* mountpoint, uint8_t device_id) { - printf("[FAT32-VFS] Mounting FAT32 on device %d at %s\n", device_id, mountpoint); + debug("[FAT32-VFS] Mounting FAT32 on device %d at %s", device_id, mountpoint); // First mount FAT32 using existing function if (fat32_mount(device_id) != 0) { - printf("[FAT32-VFS] Failed to mount FAT32 on device %d\n", device_id); + error("[FAT32-VFS] Failed to mount FAT32 on device %d", device_id); return VFS_ERROR; } diff --git a/src/kernel/ide.cpp b/src/kernel/ide.cpp index 3a443ba..84d3d33 100644 --- a/src/kernel/ide.cpp +++ b/src/kernel/ide.cpp @@ -1,5 +1,6 @@ #include "kernel/ide.h" #include "kernel/port_io.h" +#include "kernel/debug.h" #include #include @@ -23,11 +24,11 @@ static int ide_wait_ready(uint16_t base_port) { return 0; } if (status & IDE_STATUS_ERR) { - printf("[IDE] Error status: 0x%x\n", status); + error("[IDE] Error status: 0x%x", status); return -1; } } - printf("[IDE] Timeout waiting for ready\n"); + error("[IDE] Timeout waiting for ready"); return -1; } @@ -41,11 +42,11 @@ static int ide_wait_drq(uint16_t base_port) { return 0; } if (status & IDE_STATUS_ERR) { - printf("[IDE] Error status during DRQ wait: 0x%x\n", status); + error("[IDE] Error status during DRQ wait: 0x%x", status); return -1; } } - printf("[IDE] Timeout waiting for DRQ\n"); + error("[IDE] Timeout waiting for DRQ"); return -1; } @@ -94,12 +95,12 @@ int ide_identify(uint8_t drive_id, uint16_t* buffer) { int ide_read_sectors(uint8_t drive_id, uint32_t lba, uint8_t count, uint16_t* buffer) { if (drive_id >= IDE_MAX_DRIVES || !drives[drive_id].exists) { - printf("[IDE] Invalid drive: %d\n", drive_id); + error("[IDE] Invalid drive: %d", drive_id); return -1; } if (count == 0 || count > 256) { - printf("[IDE] Invalid sector count: %d\n", count); + error("[IDE] Invalid sector count: %d", count); return -1; } @@ -107,7 +108,7 @@ int ide_read_sectors(uint8_t drive_id, uint32_t lba, uint8_t count, uint16_t* bu uint16_t base_port = drive->base_port; uint8_t drive_num = drive->drive_num; - printf("[IDE] Reading %d sectors from LBA %u on drive %d\n", count, lba, drive_id); + debug("[IDE] Reading %d sectors from LBA %u on drive %d", count, lba, drive_id); ide_select_drive(base_port, drive_num); @@ -132,7 +133,7 @@ int ide_read_sectors(uint8_t drive_id, uint32_t lba, uint8_t count, uint16_t* bu // Read each sector for (int sector = 0; sector < count; sector++) { if (ide_wait_drq(base_port) != 0) { - printf("[IDE] Failed to read sector %d\n", sector); + error("[IDE] Failed to read sector %d", sector); return -1; } @@ -142,13 +143,13 @@ int ide_read_sectors(uint8_t drive_id, uint32_t lba, uint8_t count, uint16_t* bu } } - printf("[IDE] Successfully read %d sectors\n", count); + success("[IDE] Successfully read %d sectors", count); return 0; } int ide_write_sectors(uint8_t drive_id, uint32_t lba, uint8_t count, uint16_t* buffer) { // Write functionality - to be implemented later - printf("[IDE] Write functionality not yet implemented\n"); + error("[IDE] Write functionality not yet implemented"); return -1; } @@ -172,7 +173,7 @@ static int ide_detect_drive(uint16_t base_port, uint8_t drive_num) { // Calculate total sectors (LBA mode) drives[drive_count].sectors = *((uint32_t*)&identify_buffer[60]); - printf("[IDE] Drive %d detected: %u sectors (%u MB)\n", + success("[IDE] Drive %d detected: %u sectors (%u MB)", drive_count, drives[drive_count].sectors, (drives[drive_count].sectors * 512) / (1024 * 1024)); @@ -185,22 +186,22 @@ static int ide_detect_drive(uint16_t base_port, uint8_t drive_num) { } int ide_init(void) { - printf("[IDE] Initializing IDE controller\n"); + debug("[IDE] Initializing IDE controller"); drive_count = 0; memset(drives, 0, sizeof(drives)); // Detect drives on primary IDE controller - printf("[IDE] Scanning primary IDE controller (0x1F0)\n"); + debug("[IDE] Scanning primary IDE controller (0x1F0)"); ide_detect_drive(IDE_PRIMARY_BASE, IDE_DRIVE_MASTER); ide_detect_drive(IDE_PRIMARY_BASE, IDE_DRIVE_SLAVE); // Detect drives on secondary IDE controller - printf("[IDE] Scanning secondary IDE controller (0x170)\n"); + debug("[IDE] Scanning secondary IDE controller (0x170)"); ide_detect_drive(IDE_SECONDARY_BASE, IDE_DRIVE_MASTER); ide_detect_drive(IDE_SECONDARY_BASE, IDE_DRIVE_SLAVE); - printf("[IDE] Found %d drives\n", drive_count); + success("[IDE] Found %d drives", drive_count); return drive_count; } diff --git a/src/kernel/isr.cpp b/src/kernel/isr.cpp index 8df2215..4a87849 100644 --- a/src/kernel/isr.cpp +++ b/src/kernel/isr.cpp @@ -19,7 +19,7 @@ void register_interrupt_handler(uint8_t n, isr_t handler) extern "C" void isr_handler(registers_t *regs) { // Print the interrupt code (exception number) - printf("ISR Exception: Interrupt %d, Error Code: %d\n", regs->int_no, regs->err_code); + error("ISR Exception: Interrupt %d, Error Code: %d", regs->int_no, regs->err_code); // Handle critical exceptions like page faults (Interrupt 14) if(interrupt_handlers[regs->int_no]) { diff --git a/src/kernel/vfs.cpp b/src/kernel/vfs.cpp index db4f3d2..8d91210 100644 --- a/src/kernel/vfs.cpp +++ b/src/kernel/vfs.cpp @@ -1,5 +1,6 @@ #include "kernel/vfs.h" #include "kernel/fat32.h" +#include "kernel/debug.h" #include #include @@ -9,7 +10,7 @@ static char current_working_directory[VFS_MAX_PATH]; static uint8_t vfs_initialized = 0; int vfs_init(void) { - printf("[VFS] Initializing Virtual File System\n"); + debug("[VFS] Initializing Virtual File System"); // Clear mount table memset(mounts, 0, sizeof(mounts)); @@ -21,34 +22,34 @@ int vfs_init(void) { strcpy(current_working_directory, "/"); vfs_initialized = 1; - printf("[VFS] VFS initialized successfully\n"); + success("[VFS] VFS initialized successfully"); return VFS_SUCCESS; } int vfs_mount(const char* mountpoint, uint8_t fs_type, uint8_t device_id, vfs_operations_t* ops, void* fs_data) { if (!vfs_initialized) { - printf("[VFS] VFS not initialized\n"); + error("[VFS] VFS not initialized"); return VFS_ERROR; } if (!mountpoint || !ops) { - printf("[VFS] Invalid mount parameters\n"); + error("[VFS] Invalid mount parameters"); return VFS_ERROR; } - printf("[VFS] Mounting filesystem type %d at %s\n", fs_type, mountpoint); + debug("[VFS] Mounting filesystem type %d at %s", fs_type, mountpoint); // Validate filesystem type if (fs_type != VFS_FS_RAMFS && fs_type != VFS_FS_FAT32) { - printf("[VFS] Unsupported filesystem type: %d\n", fs_type); + error("[VFS] Unsupported filesystem type: %d", fs_type); return VFS_ERROR; } // Check if mountpoint already exists for (int i = 0; i < VFS_MAX_MOUNTS; i++) { if (mounts[i].mounted && strcmp(mounts[i].mountpoint, mountpoint) == 0) { - printf("[VFS] Mountpoint %s already mounted\n", mountpoint); + error("[VFS] Mountpoint %s already mounted", mountpoint); return VFS_ALREADY_MOUNTED; } } @@ -63,12 +64,12 @@ int vfs_mount(const char* mountpoint, uint8_t fs_type, uint8_t device_id, mounts[i].fs_data = fs_data; mounts[i].mounted = 1; - printf("[VFS] Successfully mounted filesystem at %s\n", mountpoint); + success("[VFS] Successfully mounted filesystem at %s", mountpoint); return VFS_SUCCESS; } } - printf("[VFS] No free mount slots\n"); + error("[VFS] No free mount slots"); return VFS_NO_SPACE; } @@ -77,7 +78,7 @@ int vfs_unmount(const char* mountpoint) { return VFS_ERROR; } - printf("[VFS] Unmounting %s\n", mountpoint); + debug("[VFS] Unmounting %s", mountpoint); // Find mount point for (int i = 0; i < VFS_MAX_MOUNTS; i++) { @@ -93,17 +94,17 @@ int vfs_unmount(const char* mountpoint) { if (mounts[i].fs_type == VFS_FS_FAT32) { int result = fat32_unmount(); if (result != 0) { - printf("[VFS] Warning: FAT32 unmount returned error %d\n", result); + error("[VFS] Warning: FAT32 unmount returned error %d", result); } } mounts[i].mounted = 0; - printf("[VFS] Successfully unmounted %s\n", mountpoint); + success("[VFS] Successfully unmounted %s", mountpoint); return VFS_SUCCESS; } } - printf("[VFS] Mountpoint %s not found\n", mountpoint); + error("[VFS] Mountpoint %s not found", mountpoint); return VFS_NOT_FOUND; } @@ -173,22 +174,22 @@ int vfs_resolve_path(const char* path, vfs_mount_t** mount, char* relative_path) int vfs_list_mounts(void) { if (!vfs_initialized) { - printf("[VFS] VFS not initialized\n"); + error("[VFS] VFS not initialized"); return 0; } - printf("[VFS] Current mount points:\n"); + debug("[VFS] Current mount points:"); int count = 0; for (int i = 0; i < VFS_MAX_MOUNTS; i++) { if (mounts[i].mounted) { - printf(" %s (type %d, device %d)\n", + debug(" %s (type %d, device %d)", mounts[i].mountpoint, mounts[i].fs_type, mounts[i].device_id); count++; } } if (count == 0) { - printf(" No filesystems mounted\n"); + debug(" No filesystems mounted"); } return count; @@ -229,7 +230,7 @@ int vfs_chdir(const char* path) { // Update current working directory with the normalized path strcpy(current_working_directory, normalized_path); - printf("[VFS] Changed directory to %s\n", current_working_directory); + success("[VFS] Changed directory to %s", current_working_directory); return VFS_SUCCESS; } @@ -243,20 +244,20 @@ int vfs_open(const char* path, vfs_file_t* file) { return VFS_ERROR; } - printf("[VFS] Opening file: %s\n", path); + debug("[VFS] Opening file: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } - printf("[VFS] Path resolved: %s -> mount=%p, relative_path='%s'\n", path, mount, relative_path); + debug("[VFS] Path resolved: %s -> mount=%p, relative_path='%s'", path, mount, relative_path); if (!mount->ops || !mount->ops->open) { - printf("[VFS] Filesystem does not support open operation\n"); + error("[VFS] Filesystem does not support open operation"); return VFS_ERROR; } @@ -270,7 +271,7 @@ int vfs_open(const char* path, vfs_file_t* file) { } if (!vfs_handle) { - printf("[VFS] No free file handles available\n"); + error("[VFS] No free file handles available"); return VFS_NO_SPACE; } @@ -281,12 +282,12 @@ int vfs_open(const char* path, vfs_file_t* file) { if (result == VFS_SUCCESS) { // Copy the VFS handle to user's file structure *file = *vfs_handle; - printf("[VFS] Successfully opened file: %s\n", path); + success("[VFS] Successfully opened file: %s", path); } else { // Clear the VFS handle on failure vfs_handle->in_use = 0; vfs_handle->mount = NULL; - printf("[VFS] Failed to open file: %s\n", path); + error("[VFS] Failed to open file: %s", path); } return result; @@ -356,18 +357,18 @@ int vfs_readdir(const char* path, vfs_dirent_t* entries, int max_entries) { return VFS_ERROR; } - printf("[VFS] Reading directory: %s\n", path); + debug("[VFS] Reading directory: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } if (!mount->ops || !mount->ops->readdir) { - printf("[VFS] Filesystem does not support readdir operation\n"); + error("[VFS] Filesystem does not support readdir operation"); return VFS_ERROR; } @@ -379,18 +380,18 @@ int vfs_mkdir(const char* path) { return VFS_ERROR; } - printf("[VFS] Creating directory: %s\n", path); + debug("[VFS] Creating directory: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } if (!mount->ops || !mount->ops->mkdir) { - printf("[VFS] Filesystem does not support mkdir operation\n"); + error("[VFS] Filesystem does not support mkdir operation"); return VFS_ERROR; } @@ -402,18 +403,18 @@ int vfs_rmdir(const char* path) { return VFS_ERROR; } - printf("[VFS] Removing directory: %s\n", path); + debug("[VFS] Removing directory: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } if (!mount->ops || !mount->ops->rmdir) { - printf("[VFS] Filesystem does not support rmdir operation\n"); + error("[VFS] Filesystem does not support rmdir operation"); return VFS_ERROR; } @@ -425,18 +426,18 @@ int vfs_create(const char* path) { return VFS_ERROR; } - printf("[VFS] Creating file: %s\n", path); + debug("[VFS] Creating file: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } if (!mount->ops || !mount->ops->create) { - printf("[VFS] Filesystem does not support create operation\n"); + error("[VFS] Filesystem does not support create operation"); return VFS_ERROR; } @@ -448,18 +449,18 @@ int vfs_remove(const char* path) { return VFS_ERROR; } - printf("[VFS] Removing file: %s\n", path); + debug("[VFS] Removing file: %s", path); vfs_mount_t* mount; char relative_path[VFS_MAX_PATH]; if (vfs_resolve_path(path, &mount, relative_path) != VFS_SUCCESS) { - printf("[VFS] Failed to resolve path: %s\n", path); + error("[VFS] Failed to resolve path: %s", path); return VFS_NOT_FOUND; } if (!mount->ops || !mount->ops->remove) { - printf("[VFS] Filesystem does not support remove operation\n"); + error("[VFS] Filesystem does not support remove operation"); return VFS_ERROR; }