From: Tulio A M Mendes Date: Fri, 13 Feb 2026 07:39:05 +0000 (-0300) Subject: fix: consolidate kconsole banner + safe disk FS probe order X-Git-Url: https://projects.tadryanom.me/docs/static/gitweb.js?a=commitdiff_plain;h=a3698e075ca713d6fb57b330e48ea7b4f0ed5f23;p=AdrOS.git fix: consolidate kconsole banner + safe disk FS probe order 1. Consolidate redundant PANIC + kconsole banners into a single message shown by kconsole_enter(). Previously both main.c and kconsole.c printed separate banners. 2. Fix critical VFS mount ordering bug: diskfs, FAT, and ext2 all share the same ATA primary master disk at LBA 0. Previously diskfs_create_root() was called first, and its auto-format (diskfs_super_load) would overwrite LBA 2-3 if DISKFS_MAGIC was not found -- destroying any ext2 superblock at the same location. Now the probe order is: a) ext2 (checks magic 0xEF53 at byte 1080) b) FAT (validates BPB fields at LBA 0) c) diskfs (fallback, may auto-format blank disks) Only one FS is mounted per device -- they are mutually exclusive. 3. Make ata_pio_init_primary_master() idempotent with a static guard flag so it can be called from both init.c and diskfs_create_root() without double-initializing. 4. Increase VFS mount table from 8 to 16 slots to prevent -ENOSPC when adding future mount points. Build: clean, cppcheck: clean, smoke: 19/19 pass --- diff --git a/src/hal/x86/ata_pio.c b/src/hal/x86/ata_pio.c index 70799ef..ee79bd0 100644 --- a/src/hal/x86/ata_pio.c +++ b/src/hal/x86/ata_pio.c @@ -68,7 +68,11 @@ uint32_t ata_pio_sector_size(void) { return 512; } +static int ata_pio_inited = 0; + int ata_pio_init_primary_master(void) { + if (ata_pio_inited) return 0; + // Register IRQ 14 handler early to prevent INTRQ storm register_interrupt_handler(46, ata_pio_irq14_handler); @@ -102,6 +106,7 @@ int ata_pio_init_primary_master(void) { kprintf("[ATA] Using PIO mode (DMA unavailable).\n"); } + ata_pio_inited = 1; return 0; } diff --git a/src/kernel/fs.c b/src/kernel/fs.c index 158b6ea..6b6b701 100644 --- a/src/kernel/fs.c +++ b/src/kernel/fs.c @@ -10,7 +10,7 @@ struct vfs_mount { fs_node_t* root; }; -static struct vfs_mount g_mounts[8]; +static struct vfs_mount g_mounts[16]; static int g_mount_count = 0; static int path_is_mountpoint_prefix(const char* mp, const char* path) { diff --git a/src/kernel/init.c b/src/kernel/init.c index f2c5562..0c60cf6 100644 --- a/src/kernel/init.c +++ b/src/kernel/init.c @@ -22,6 +22,7 @@ #include "keyboard.h" #include "console.h" +#include "ata_pio.h" #include "hal/mm.h" #include @@ -96,29 +97,46 @@ int init_start(const struct boot_info* bi) { (void)vfs_mount("/persist", persist); } - fs_node_t* disk = diskfs_create_root(); - if (disk) { - (void)vfs_mount("/disk", disk); - } - fs_node_t* proc = procfs_create_root(); if (proc) { (void)vfs_mount("/proc", proc); } - /* Probe second IDE disk partition (LBA 0) for FAT or ext2. - * The primary disk is used by diskfs; a second partition could - * be formatted as FAT or ext2 and mounted at /mnt. */ - { - fs_node_t* fatfs = fat_mount(0); - if (fatfs) { - (void)vfs_mount("/fat", fatfs); + /* Probe the ATA primary master disk and mount the appropriate + * filesystem. ext2, FAT, and diskfs are mutually exclusive on + * the same device — we probe in order of specificity so that + * diskfs auto-format never overwrites a recognized filesystem. + * + * Probe order: ext2 (magic 0xEF53), FAT (BPB validation), diskfs. + * If neither ext2 nor FAT is detected, diskfs initializes and may + * auto-format the disk. */ + if (ata_pio_init_primary_master() == 0) { + int disk_mounted = 0; + + /* 1. Try ext2 — superblock at byte 1024 (LBA 2), magic at offset 56 */ + { + fs_node_t* ext2fs = ext2_mount(0); + if (ext2fs) { + (void)vfs_mount("/ext2", ext2fs); + disk_mounted = 1; + } } - } - { - fs_node_t* ext2fs = ext2_mount(0); - if (ext2fs) { - (void)vfs_mount("/ext2", ext2fs); + + /* 2. Try FAT — BPB at LBA 0 */ + if (!disk_mounted) { + fs_node_t* fatfs = fat_mount(0); + if (fatfs) { + (void)vfs_mount("/fat", fatfs); + disk_mounted = 1; + } + } + + /* 3. Fallback to diskfs (may auto-format blank/unrecognized disks) */ + if (!disk_mounted) { + fs_node_t* disk = diskfs_create_root(); + if (disk) { + (void)vfs_mount("/disk", disk); + } } } diff --git a/src/kernel/kconsole.c b/src/kernel/kconsole.c index 03a301f..d6a17bc 100644 --- a/src/kernel/kconsole.c +++ b/src/kernel/kconsole.c @@ -348,8 +348,8 @@ static void kconsole_exec(const char* cmd) { void kconsole_enter(void) { keyboard_set_callback(0); - kc_puts("\n*** AdrOS Kernel Console (kconsole) ***\n"); - kc_puts("Type 'help' for available commands.\n"); + kc_puts("\n[PANIC] Userspace init failed -- dropping to kconsole.\n"); + kc_puts(" Type 'help' for commands, 'reboot' to restart.\n\n"); char line[KCMD_MAX]; diff --git a/src/kernel/main.c b/src/kernel/main.c index 8a9cafc..c36d401 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -82,8 +82,6 @@ void kernel_main(const struct boot_info* bi) { int init_ret = init_start(bi); if (init_ret < 0) { - console_write("\n[PANIC] Userspace init failed -- dropping to emergency console.\n"); - console_write(" (type 'help' for commands, 'reboot' to restart)\n\n"); kconsole_enter(); }