]> 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 0dd3a388a7e7cffa2a04be69ebc43ab76186f319..2b2bd84eee82989b62f4eb1df9ed5768d06f1d1e 100644 (file)
@@ -77,7 +77,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);
 
@@ -111,6 +115,7 @@ int ata_pio_init_primary_master(void) {
         kprintf("[ATA] Using PIO mode (DMA unavailable).\n");
     }
 
+    ata_pio_inited = 1;
     return 0;
 }
 
index 85af3b45cad8151320fed92c0b5ff90111795772..1fa1100438ba955342946ce1e89d09c20bf01d77 100644 (file)
@@ -19,7 +19,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 ea87a190c1842faa33b127d887d9e707ad047c1e..8a8b6bd3bb64dc0034c5aa47a1426ce1769cbe12 100644 (file)
@@ -31,6 +31,7 @@
 #include "keyboard.h"
 #include "console.h"
 
+#include "ata_pio.h"
 #include "hal/mm.h"
 
 #include <stddef.h>
@@ -105,29 +106,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 9062188581b35c977dca53d3baa64774400146df..b5f3aed946de99233416ff37f78c925f3f77aeb2 100644 (file)
@@ -357,8 +357,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 c1986cde1f7695fdfd2aa1bc77b369271678d759..5d65be1e09d4f13864f82b8077b7b5006d2a0ffd 100644 (file)
@@ -91,8 +91,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();
     }