]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: consolidate kconsole banner + safe disk FS probe order
authorTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 07:39:05 +0000 (04:39 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 07:39:05 +0000 (04:39 -0300)
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

src/hal/x86/ata_pio.c
src/kernel/fs.c
src/kernel/init.c
src/kernel/kconsole.c
src/kernel/main.c

index 70799ef32b5dfae9d0993fc183520636b25c25c5..ee79bd0a970840e6f73c766afedcf3b0a45f2cb6 100644 (file)
@@ -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;
 }
 
index 158b6ea726bbfca3df95dc629f0589028c586333..6b6b701871ac53014882a472653df783831be500 100644 (file)
@@ -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) {
index f2c5562d06a938735e2a4bc21a165f70fc00caa6..0c60cf65515faccc5b8cd33642a3830e7c783a70 100644 (file)
@@ -22,6 +22,7 @@
 #include "keyboard.h"
 #include "console.h"
 
+#include "ata_pio.h"
 #include "hal/mm.h"
 
 #include <stddef.h>
@@ -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);
+            }
         }
     }
 
index 03a301ff774803007f821d5228661222069cc667..d6a17bc7bf9a43447bb39b179a36c11f54cf16ae 100644 (file)
@@ -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];
 
index 8a9cafcacc4d22d290654c380901fe82ade3ebdf..c36d401867118e4b0cb56fc0260c353f5565f6e5 100644 (file)
@@ -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();
     }